动态生成的ajax数据从jQuery发布到PHP

时间:2016-11-03 12:08:45

标签: php jquery ajax

我有一个生成动态原始表的表,我需要通过ajax发送来从PHP接收。下面我给出了我选择的所有代码,但这不起作用。

在显示为

的控制台中
  

PHP错误:未定义索引all_ch_no,all_yd_stock_no,all_comments

HTML FORM,下面的表行是通过ajax动态生成的,如果我放置静态表数据,那么它正在工作!我怎么解决?

<form method="post" id="customer_do_add">
   <input type="text" id="phone_no" name="phone_no">

    <table class="table" id="group_cars">
        <thead>
            <tr>
                <th style="width: 20%;">Chassis No</th>
                <th style="width: 15%;">Yard Stock No</th>
                <th style="width: 50%;">Comments</th>
                <th style="width: 15%;">Remove</th>
            </tr>
            <tr>
                <td><input type="text" class="waz" value="DD51T-224534" name="all_ch_no[]"></td>
                <td><input type="text" class="waz"  value="77832" name="all_yd_stock_no[]"></td>
                <td><input type="text" class="waz"  value="Test3" name="all_comments[]"></td>
            </tr>
            <tr>
                <td><input type="text" class="waz" value="DD51T-45354" name="all_ch_no[]"></td>
                <td><input type="text" class="waz"  value="123123" name="all_yd_stock_no[]"></td>
                <td><input type="text" class="waz"  value="Test" name="all_comments[]"></td>
            </tr> 
            ...
        </thead>
    </table>
</form>

jQuery的:

  $('#customer_do_add').on('submit', function(e) {
        e.preventDefault();
        var val = $('#customer_do_add').serializeArray();
            $.ajax({
                url: "auction/customer_do_add/",
                data:val,
                type: "POST",
                success: function (responseText, statusText, xhr, $form) {
                    result = $.parseJSON(responseText);
                }
            });
            return false;
    });

PHP

public function customer_do_add(){
            print_r($_POST); 
            exit;
}

仅打印电话号码,数组未打印

Array
(
    [phone_no] => 123456
)

2 个答案:

答案 0 :(得分:0)

在Jquery调用中发送数据参数,然后只有你可以收到帖子

 $('#customer_do_add').on('submit', function(e) {
    e.preventDefault();
    var val = $('#customer_do_add').serializeArray();
        $(this).ajaxSubmit({
            url: "auction/customer_do_add/",
            data: val,
            success: function (responseText, statusText, xhr, $form) {
                result = $.parseJSON(responseText);
            }
        });
        return false;
});

答案 1 :(得分:0)

你的$ _POST是空的,因为jQuery.ajax()会将数据作为GET数据传递(它被附加到url)。将方法arg设置为post将解决此问题。

$.ajax({
    url: "/auction/customer_do_add/",
    data: val,
    success: function (data) {
        console.log(data);
    },
    dataType: 'json',
    method: 'POST',
});

您还可以使用jQuery.post()来获得简化的语法;

$.post({
    url: "/auction/customer_do_add/",
    data: val,
    success: function (data) {
        console.log(data);
    },
    dataType: 'json'
});