【问题标题】:Form serialize with ajax post使用ajax post进行表单序列化
【发布时间】:2017-03-25 19:06:39
【问题描述】:

我试图在 ajax 函数中发布带有 onclick 事件的表单数据。但数据没有发布在下一页中。我正在使用来自数据提交的序列化方法,但它不起作用。同时,当我使用 jQuery .val 函数时,它会在右侧字段中捕获正确的数据

这是我的html:

             <form id="customer_from"  class="form-group">
                <div class="row">
                   <div class="col-lg-2">
                      <div id="customer_information" style="background-color:grey;padding:5px;text-align:center;border:1px solid white;cursor: pointer; cursor: hand;height:50px;font-size:15px;" data-registry-id="<?php echo $_POST['order_number']; ?>" data-order-ref="<?php echo $_POST['order_ref'];  ?>" data-registry-type="<?php echo $_POST['registry_type'];  ?>" data-customer-id="<?php echo $data['customer_id'];?>">
                         Customer Information
                      </div>
                      <div id="registry_details" style="background-color:grey;padding:5px;text-align:center;border:1px solid white;cursor: pointer; cursor: hand;height:50px;font-size:15px;" data-registry-id="<?php echo $_POST['order_number']; ?>" data-order-ref="<?php echo $_POST['order_ref'];  ?>" data-registry-type="<?php echo $_POST['registry_type'];  ?>" data-customer-id="<?php echo $data['customer_id'];?>">
                         Registry Details
                      </div>
                      <div id="send_email" style="background-color:grey;padding:5px;text-align:center;border:1px solid white;cursor: pointer; cursor: hand;height:50px;font-size:15px;"  data-registry-id="<?php echo $_POST['order_number']; ?>" data-order-ref="<?php echo $_POST['order_ref'];  ?>" data-registry-type="<?php echo $_POST['registry_type'];  ?>" data-customer-id="<?php echo $data['customer_id'];?>">
                         E-mail
                      </div>
                      <div id="add_comment" style="background-color:grey;padding:5px;text-align:center;border:1px solid white;cursor: pointer; cursor: hand;height:50px;font-size:15px;" data-registry-id="<?php echo $_POST['order_number']; ?>" data-order-ref="<?php echo $_POST['order_ref'];  ?>" data-registry-type="<?php echo $_POST['registry_type'];  ?>" data-customer-id="<?php echo $data['customer_id'];?>">
                         Add Comment
                      </div>
                   </div>
                   <div class="col-lg-10" id="dynamic" >
                      <div class="col-lg-5">
                         <label for='customer_id'> Customer ID: <span class='required-field'>*</label>
                         <input name='customer_id'  type='text' value="<?php echo $data['customer_id'] ?>" class='form-control' required /> </span>
                         <label for='customer_name'> Customer Name: <span class='required-field'>*</label>
                         <input name='customer_name'  type='text' value="<?php echo $data['firstname'] ?>" class='form-control' required /> </span>
                         <label for='customer_email'> E-mail: <span class='required-field'>*</label>
                         <input name='customer_email'  type='text' value="<?php echo $data['email'] ?>" class='form-control' required /> </span>
                         <label for='customer_telephone1'> Telephone#1: <span class='required-field'>*</label>
                         <input name='customer_telephone1'  type='text' value="<?php echo $data['phone'] ?>" class='form-control' required /> </span>
                         <label for='customer_telephone2'> Telephone#2:<span class='required-field'>*</label>
                         <input name='customer_telephone2'  type='text' value="<?php echo $data['alt_phone'] ?>" class='form-control' required /> </span>
                         <label class="checkbox-inline"><input type="checkbox" value="yes">Pattern</label>
                      </div>
                      <div class="col-lg-5">
                         <label for='customer_address'> Address: <span class='required-field'>*</label>
                         <input name='customer_address'  type='text' value="<?php echo $data['address'] ?>" class='form-control' required /> </span>
                         <label for='customer_city'> City: <span class='required-field'>*</label>
                         <input name='customer_city'  type='text' value="<?php echo $data['city'] ?>" class='form-control' required /> </span>
                         <label for='customer_province'> Province: <span class='required-field'>*</label>
                         <input name='customer_province'  type='text' value="<?php echo $data['province'] ?>" class='form-control' required /> </span>
                         <label for='customer_postal'> Postal code: <span class='required-field'>*</label>
                         <input name='customer_postal'  type='text' value="<?php echo $data['postal_code'] ?>" class='form-control' required /> </span>
                         <label for='customer_country'> Country: <span class='required-field'>*</label>
                         <select id = "customer_country" name="customer_country" class="form-control" title="Select a Category" required>
                            <?php echo $customer_country; ?>
                         </select>
                         <label class="checkbox-inline"><input type="checkbox" value="yes">Newsletter</label>
                      </div>
                   </div>
                </div>

                <div class="row">
                   <div class="col-sm-10 col-xs-11 col-md-12">
                      <input type="button" class="btn btn-success" id="update-registry" value="Update Registry" style="float:right;">
                      <input type="button" class="btn btn-danger" id="delete-registry" data-registry="<?php echo $_POST['order_ref'];?>" data-type="<?php echo $_POST['registry_type']; ?>" value="Delete" style="float:left;">
                   </div>
                </div>
             </div>
          </form>

这里是 ajax 调用:

$("#update-registry").on("click",function(){
        $.ajax({
        type: "POST",
        url: "ajax/update_customer.php",
        data:$("from#customer_form").serializeArray(),
        beforeSend: function() {

        },
        success: function(msg) {
            $("#registry").find(".update_registry").html(msg);
        },
        error: function() {
            alert("failure");
        }
    });
})

【问题讨论】:

  • “它不工作”永远是不够的信息。你期待什么行为?实际发生了什么?请求是否发送?请求中的数据是什么? PHP代码中有错误吗?您是否检查过服务器日志或客户端控制台?帮助我们为您提供帮助。
  • 发送的请求已完成,但是当我去查看通过的变量时,它只是空白,我将其发送到空白的 php 文件,因此 php 中没有错误,我正在检查控制台看到帖子标签@RoryMcCrossan

标签: javascript php jquery ajax


【解决方案1】:

查看表单 ID:

<form id="customer_from" 

有一个错字:

 data:$("from#customer_form").serializeArray(),
 //------^^^^----------^^^^should be from
 //------^^^^should be form

from 更改为form。更好的是,ID 是唯一的,因此无需在标签名称前加上前缀:

 data:$("#customer_from").serializeArray(),

【讨论】:

  • 仍然一无所获@Jai
  • 现在有什么问题?检查网络选项卡中的请求,看看是否在表单数据中找到任何内容?
  • 同样没有数据在 ajax 调用中发布并从网络选项卡检查,但参数选项卡中没有数据是问题
  • 您确定没有相同 ID 的重复表单吗?有可能。
  • @Etherealsoul 实际上表单 id 也有一个错字:customer_form 必须是 customer_from
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-23
相关资源
最近更新 更多