使用ajax的post方法发送表单数据时在PHP脚本中获取空数组

问题描述 投票:0回答:0

我正在使用serialize()方法来形成url编码字符串,我尝试搜索每个解决方案但没有成功,下面是我的代码

<form id="idEditTaskForm">
    <div class="form-group">
        <input type="text" class="form-control"  name="taskName" id="idTaskName" placeholder="Enter Task name" value="">
    </div>
    <div>
        <input type="hidden"  name="id" id="idTask" value="" />
    </div>
    <button type="submit" class="btn btn-primary" id="idEditTask">
        Save
    </button>
</form>
$(document).on('click', "#idEditTask", function(){
    $("#idCloseButton").click();
        $('#idEditTaskForm').submit(function(e) { // handle the submit event
            e.preventDefault();
            var formData = $(this).serialize();
            $.post({
                url: "http://localhost:8000/index.php?sFlag=saveTaskDetails",
                type: 'POST',
                data: {'formData': formData},
                contentType: 'application/x-www-form-urlencoded',
                success:function(data){
                    console.log(data);
                    if(data == true){
                        console.log(data);
                    }
                }
            })
        })
});

<?php
    if(isset($_GET['sFlag'])){
        $sFlag = $_GET['sFlag'];
            if($sFlag == "saveTaskDetails"){
                print_r( $_POST);
                $id = $_POST['id'];
                $name = $_POST['taskName'];

                //$oCrudData = new CrudData();
                //$result = $oCrudData->saveTask($id, $name);
                //echo $result;
            }
    }
?>

当我打印 $_POST 请求数据时,显示空数组和未定义索引错误

数组 ( ) 注意:未定义索引:id 在 C:\Users\Phaneeswar D\Desktop\Suman_learning_project

php jquery ajax
© www.soinside.com 2019 - 2024. All rights reserved.