drq61040 2016-02-02 10:39
浏览 50
已采纳

表格序列化与ajax帖子

I was trying to post the form data with onclick event in an ajax function.but the data are not being posted in the next page. I was using the serialize method of from data submission but its not working. Meanwhile when I use jQuery .val function it captures the right data in th right field

Here is my 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>

and here is the ajax call:

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

  • dpir3950 2016-02-02 10:45
    关注

    See the form ID:

    <form id="customer_from" 
    

    There is a typo:

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

    change from to form. Even better as IDs are unique so there is no need to prefix the tag name:

     data:$("#customer_from").serializeArray(),
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法