这是我的 ajax 表单,它可以工作,
$.ajax({
type: "POST",
url: "update_coordinate.php",
data:
'id=' +$id+
'name=' +$name+
'wname=' +$wname+
'xcor=' +$xcor+
'ycor=' +$ycor+
'xwid=' +$xwid+
'yhei=' +$yhei+
'photo=' +$photo+
'targeturl=' +$targeturl,
success: function(data){
alert(
' id:' +$id+
' name:' +$name+
' wname:' +$wname+
' xcor:' +$xcor+
' ycor:' +$ycor+
' xwid:' +$xwid+
' yhei:' +$yhei+
' photo:' +$photo+
' targeturl:' +$targeturl
);
alert(data);
}
});
警报显示所有数据,问题是 php 端似乎没有读取值,我不确定我是否遗漏了什么。
这很有趣:在四处乱逛时,我以某种方式将整个连接的字符串输入到名称字段中,其中包括等号……这怎么可能发生?
我取出了 if(post) 参数,该参数在使用方法 post 和提交按钮将 html 表单提交到同一页面时使用。我不知道那是不是很糟糕。
这两个我都试过了
if(isset($_POST['id'])){
$id = $_POST['id'];
}else {
$id = "";
}
$name = $_POST['name'];
获取错误 Undefined index: name 其余都很好,因为我指定了一个空的任意字符串值
我错过了什么?
最佳答案
使用 jQuery 发布:
$.post( "update_coordinate.php", {
'id':$id,
'name':$name,
'wname':$wname,
'xcor':$xcor,
'ycor':$ycor,
'xwid':$xwid,
'yhei':$yhei,
'photo':$photo,
'targeturl':$targeturl
}).done(function( data ) {
alert( "Server Response: " + data );
});
因为这将正确发送 POST 数据,因为每次我尝试使用 ajax 并有一个数据字符串时,它似乎总是作为 GET 发送,因为服务器认为它是一个 url/查询字符串。
此外,通过执行上述操作......您可以轻松地将 JS 数组发送到 PHP 或其他对象。
关于javascript - 无法检索从 AJAX 发送的 POST 数据, undefined index php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28755481/