我正在尝试提交一个模态 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);