douchou8935 2013-03-25 02:16
浏览 43
已采纳

jQuery模态表单提交

I am attempting to submit a modal jQuery form. I took the code pretty much from the site with just a few tweaks and moved the javascript to its own file.

I am struggling with getting the form information to pass to my php script which I am attempting through AJAX. When I pull the serializedArray() of the form I get [object Object]for each element in the form. I've simplified the php to just the email field until I can get this working. Am I misunderstanding the logic behind this?

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 is the form validation and works as expected

$(  "#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条回答 默认 最新

  • douxitao8170 2013-03-25 02:19
    关注

    Use var data_string = $( "#sign_up_form" ).serialize(); instead of var data_string = $( "#sign_up_form" ).serializeArray(); . serializeArray() returns an array not a query string

    .serialize()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后的密码
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 UE5#if WITH_EDITOR导致打包的功能不可用
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题