在PHP中,AJAX post值为空

时间:2016-01-11 07:57:13

标签: php jquery ajax

我正在尝试将值发布到另一个页面并执行一些mysql操作,但这些值是作为空对象发布的。这是我的index.php文件的脚本部分:

$rep_date = $_POST['date'];
$date = date("yyyy-mm-dd",strtotime($rep_date));
$name = $_POST['name'];
$sql = mysql_query("SELECT * FROM infra.prob_report WHERE prob_rept_name = '$name'");
$rows = array();
while($row = mysql_fetch_array($sql)) {
    $nestedData=array(); 
    $nestedData[] = $row["rep_id"];
    $nestedData[] = $row["prob_rept_date"];
    $nestedData[] = $row["prob_equip_name"];
    $nestedData[] = $row["prob_rept_name"];
    $nestedData[] = $row["prob_desc"];
    $data[] = $nestedData;
}
echo json_encode($data);

这是我应该处理我发布的值的PHP页面,new_prob_submit.php:

<system.serviceModel>
<extensions>
  <behaviorExtensions>.........

4 个答案:

答案 0 :(得分:4)

问题是你没用过

dataType:"json"中的

ajax

 $.ajax({
            type: "POST",
            url: "new_prob_submit.php", 
            //added type
            dataType:"json",
            data: { 
                'date': $('#picker').val(), 
                'name': $('#name').val() 
            },
            success: function()    {
                alert("success");
            }
        });

请检查Ajax

答案 1 :(得分:3)

 $.ajax({
                type:"POST",
                url: "new_prob_submit.php", 
                data: { 
                    'date': $('#picker').val(), 
                    'name': $('#name').val() 
                },
                dataType: "json",
                success:function(data){
                     alert(data);
                }
            });

答案 2 :(得分:1)

您使用的type: 'POST'不正确,method: 'POST'对象中必须为$.ajax。确保console.log($('#picker').val(), $('#name').val() )在控制台中获取值。

了解更多here

答案 3 :(得分:0)

将此脚本添加到processData:true,如

  $.ajax({url: "submit.php",
       data: values,
       method: 'POST',
       processData: true,
       success: function (data) {
       alert(data);}});
相关问题