【发布时间】:2018-11-09 01:12:25
【问题描述】:
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 帖子,但我找不到任何使它起作用的东西,也许我是问题,但我没有其他人为我单击按钮...也许我只是强调自己所以在这方面我看不到明显的东西。感谢您的帮助。
【问题讨论】:
-
我刚刚测试了您的样本(使用模拟数据和一种表格)并成功了。您能否在“网络”选项卡下选择 POST 请求的情况下发布 Chrome 开发人员在“标题”选项卡下显示的 FormData?
-
你如何运行
goals()?它在onsubmit的一个表格中吗?确保您阻止了正常的表单提交。 -
goals() 当前由按钮运行但我尝试从表单内部提交它,给出相同的空查询,设置了 e.preventdefault。编辑:实际上 preventdefault 不是必需的,因为我使用 jQuery 获取值而不使用默认提交