我正在尝试提交模态 jQuery 表单。我几乎从网站上获取了代码,只做了一些调整,然后将 javascript 移到了它自己的文件中。
我正在努力获取要通过 AJAX 传递给我的 php 脚本的表单信息。当我拉取表单的 serializedArray() 时,我得到表单中每个元素的 [object Object]。我已将 php 简化为仅电子邮件字段,直到我可以正常工作。我是不是误解了这背后的逻辑?
HTML
<div id="dialog-form">
<form id="sign_up_form" name="sign_up_form" method="post">
<fieldset>
<label for="name">Name*</label>
<input type="text" name="name" id="name" class="text ui-widget-content ui-corner-all" />
<label for="email">Email*</label>
<input type="text" name="email" id="email" value="" class="text ui-widget-content ui-corner-all" />
<label for="password">Password*</label>
<input type="password" name="password" id="password" value="" class="text ui-widget-content ui-corner-all" />
<label for="code">Code</label>
<input type="code" name="code" id="code" value="" class="text ui-widget-content ui-corner-all" />
</fieldset>
</form>
</div>
JQuery - bValid 是表单验证并按预期工作
$( "#dialog-form" ).dialog({
autoOpen: false,
height: 300,
width: 350,
modal: true,
buttons: {
"Create an account": function() {
var data_string = $( "#sign_up_form" ).serializeArray();
alert(data_string);
if ( bValid ) {
$.ajax({
type: "POST",
url: url,
dataType: "json",
data: data_string,
success: function(data){
alert(data)
}
});
$( this ).dialog( "close" );
}
},
Cancel: function() {
$( this ).dialog( "close" );
}
},
close: function() {
allFields.val( "" ).removeClass( "ui-state-error" );
}
});
PHP
if (isset($_POST['email'])) {
$jsonReceiveData = $_POST['email'];
}
else{
$jsonReceiveData = "didn't pass";
}
echo json_encode($jsonReceiveData);
最佳答案
使用 var data_string = $( "#sign_up_form").serialize();
而不是 var data_string = $( "#sign_up_form").serializeArray();
. serializeArray()返回一个数组而不是查询字符串
关于php - jQuery 模态表单提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15606639/