php - jQuery 模态表单提交

标签 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);

最佳答案

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

.serialize()

关于php - jQuery 模态表单提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15606639/

相关文章:

javascript - JW Player smarty 不工作

php多维数组获取值

php - 如何将 php 变量从 View (html)发送到 Controller

javascript - 在 Cloud9IDE 上用 PHP 创建新文件

php - 无法从 PHP 执行 Bash 脚本

Zend Framework 中的 AJAX 分页

Javascript onsubmit() 函数不执行任何操作

jquery - 500(内部服务器错误)ajax 和 laravel

jquery - 仅使用 HTML/JS/CSS - 有没有办法以黑白方式渲染彩色图像?

jquery - 3 panel scrollto 窗口左右滚动但从中间开始