表格序列化与ajax帖子

时间:2016-02-02 10:39:32

标签: javascript php jquery ajax

我试图在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");
        }
    });
})

1 个答案:

答案 0 :(得分:1)

请参阅表格ID:

<form id="customer_from" 

有一个拼写错误:

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

from更改为form。更好的是,ID是唯一的,因此不需要为标记名称添加前缀:

 data:$("#customer_from").serializeArray(),
相关问题