将ajax数据序列化到php
我正在努力从php代码中的ajax post读取post数据。 我既不是PHP专家,也不是Jquery专家,我是在几周前学会的。很抱歉,如果我还没有使用所有的术语。我从ajax序列化数据,因为我的表单中会有更多字段。为了清楚起见,这里只显示一个字段。 我试图读取每个变量,例如本例中的注释, 我只是尝试打印r($\u POST),结果出现了错误。 我看不出该怎么做,可能是转换或语法错误。 如果您有任何见解,我将不胜感激 php文件将ajax数据序列化到php,php,jquery,ajax,Php,Jquery,Ajax,我正在努力从php代码中的ajax post读取post数据。 我既不是PHP专家,也不是Jquery专家,我是在几周前学会的。很抱歉,如果我还没有使用所有的术语。我从ajax序列化数据,因为我的表单中会有更多字段。为了清楚起见,这里只显示一个字段。 我试图读取每个变量,例如本例中的注释, 我只是尝试打印r($\u POST),结果出现了错误。 我看不出该怎么做,可能是转换或语法错误。 如果您有任何见解,我将不胜感激 php文件 public function ajaxSave($post_id
public function ajaxSave($post_id)
{
print_r($_POST);
}
jquery脚本
$('body').on('click','#saveComment',function(e) {
$("#comment-form").submit(function(e) {
var postData = $(this).serialize();
alert(postData);
$.ajax(
{
url : "ajaxSave.php",
type: "POST",
data : postData,
dataType:"json",
success:function(data)
{
alert('success');
},
error: function(jqXHR, textStatus, errorThrown)
{
alert("error");
}
});
e.preventDefault(); //STOP default action
});
$("#comment-form").submit();
});
表格
<input id="Comment_comment" type="text" name="Comment[comment]" maxlength="140" size="60">
来源
Comment%5Bcomment%5D=mycomment&Comment%5Bpost_id%5D=16
在HTML标记中,我有
Comment[comment] mycomment
Comment[post_id] 16
Array ( [Comment] => Array ( [comment] => for [post_id] => 16 ) )
您希望返回JSON,但返回的是
$\u POST
超全局的打印,这是一个错误
删除数据类型并将打印输出记录到控制台以查看它
$('body').on('click', '#saveComment', function (e) {
e.preventDefault();
$("#comment-form").trigger('submit');
});
$("#comment-form").submit(function(e) {
e.preventDefault();
var postData = $(this).serialize();
alert(postData);
$.ajax({
url: "ajaxSave.php",
type: "POST",
data: postData,
success: function (data) {
console.log(data)
},
error: function (jqXHR, textStatus, errorThrown) {
alert("error");
}
});
});
并且不要将提交事件绑定到单击事件中Replace
print\r($\u POST)代码>带有echo json\u编码($\u POST)代码>你忘了引用url参数是的,谢谢,在我的代码中它是编码的,因此调用php代码,我在我的重新描述@adeneo时犯了一个错误,但是错误仍然存在当然了你得到了什么错误?我讨厌名称、类和ID中的大写字母。就像黑板上的钉子。告诉我们PHP错误