jQuery模态表单提交

时间:2013-03-25 02:16:49

标签: php jquery ajax

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

1 个答案:

答案 0 :(得分:6)

使用var data_string = $( "#sign_up_form" ).serialize();代替var data_string = $( "#sign_up_form" ).serializeArray();serializeArray()返回的数组不是查询字符串

.serialize()