繁体   English   中英

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

[英]$.ajax type post is empty in PHP $_POST

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返回空字段,甚至包括电子邮件,并且var_dump($_POST)也返回array(0) {}

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

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

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

$notificationStart  = $_POST["notificationEnd"];

但是您的Ajax通话正在发送

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM