$ .ajax类型的帖子在PHP $ _POST中为空

时间:2018-11-08 16:30:46

标签: php ajax

ajax请求在javascript部分工作正常,但是在PHP中,POST会被删除,并且不会仅传递一个值。

ajax看起来像这样:

function goals() { //dynamically added forms with their corresponding inputs to be posted
    setForms.forEach(function(formIndex) { //the forms that are added and their index
        var formData = { 
            'email': "<?php echo $_SESSION["email"]; ?>", //session email works fine
            'goalRow': formIndex,
            'motivation': $("#goals"+formIndex+"-1").val(), //formIndex works fine adding the right number to get the values
            'priority': $("#goals"+formIndex+"-2").val(),
            'goal': $("#goals"+formIndex+"-3").val(),
            'measure': $("#goals"+formIndex+"-4").val(),
            'endDate': $("#goals"+formIndex+"-5").val(),
            'phase1': $("#goals"+formIndex+"-6").val(),
            'phase2': $("#goals"+formIndex+"-7").val(),
            'phase3': $("#goals"+formIndex+"-8").val(),
            'notes': $("#goals"+formIndex+"-9").val(),
            'notification': $("#goals"+formIndex+"-10").val(),
            'frequency': $("#goals"+formIndex+"-11").val(),
            'notificationStart': $("#goals"+formIndex+"-12").val(),
        };
        $.ajax({ //posting the form
            type: "POST",
            url: "?service=goalsabroad-api", //page for the action
            data: formData, //data object
            success: function() { //log to make sure data is passed
                console.log(formData); 
            }
        });
    });
}

数据已正确通过,并且在登录所有格式时都不会出现任何问题。但是,当我进入PHP页面时,它显示Undefined index:

Chrome开发者标签显示状态200并正确记录。在新标签页中打开时,我看到PHP错误。 PHP看起来像这样:

<?php

var_dump($_POST);


$email              = $_POST["email"];
$goalRow            = $_POST["goalRow"];
$motivation         = $_POST["motivation"];
$priority           = $_POST["priority"];
$goal               = $_POST["goal"];
$measure            = $_POST["measure"];
$endDate            = $_POST["endDate"];
$phase1             = $_POST["phase1"];
$phase2             = $_POST["phase2"];
$phase3             = $_POST["phase3"];
$notes              = $_POST["notes"];
$notification       = $_POST["notification"];
$frequency          = $_POST["frequency"];
$notificationStart  = $_POST["notificationEnd"];

$query = "SELECT email FROM goalsabroad WHERE email = '$email' AND goalRow = '$goalRow'";
$result = mysqli_query($connect, $query);

if (mysqli_num_rows($result) === 0) {
    echo "empty";
   $query = "INSERT INTO goalsabroad (email, goalRow, motivation, priority, goal, measure, endDate, phase1, phase2, phase3, notes, notification, frequency, notificationStart) 
                    VALUES ('$email', '$goalRow', '$motivation', '$priority', '$goal', '$measure', '$endDate', '$phase1', '$phase2', '$phase3', '$notes', '$notification', '$frequency', '$notificationSart')";
   echo $query;
   mysqli_query($connect, $query);
} else {
    $query = "UPDATE goalsabroad SET motivation = '$motivation' 
                                 AND priority = '$priority' 
                                 AND goal = '$goal' 
                                 AND measure = '$measure'
                                 AND endDate = '$endDate'
                                 AND phase1 = '$phase1'
                                 AND phase2 = '$phase2'
                                 AND phase3 = '$phase3'
                                 AND notes = '$notes'
                                 AND notification = '$notification'
                                 AND frequency = '$frequency'
                                 AND notificationStart = '$notificationStart'
                                 WHERE email = '$email' AND goalRow = '$goalRow'";
    echo $query;
    mysqli_query($connect, $query);
}
?>

ajax发布必须成功,因为运行了查询并且按预期方式添加了新行,但只有电子邮件字段将添加到查询中的其他所有内容都是空的(echo $query返回空字段,甚至是电子邮件,而{{1 }}还会返回var_dump($_POST)

.htaccess文件将.php重定向为“无扩展名”格式,这就是为什么url像本文中的其他所有参数一样都没有.php(但是我都尝试过)的原因。

我基本上检查了有关此主题的所有stackoverflow帖子,但找不到任何使它起作用的方法,也许是我的问题,但是我没有其他人为我单击按钮...也许我只是在强调自己在这一点上,我看不到明显的东西。谢谢您的帮助。

1 个答案:

答案 0 :(得分:2)

在您的PHP脚本中,您具有:

$notificationStart  = $_POST["notificationEnd"];

但是您的Ajax通话正在发送

'notificationStart': $("#goals"+formIndex+"-12").val(),

寻找$ _POST ['notificationEnd']时,它将是未定义的索引。