【问题标题】:JQUERY Ajax - POST Data passed with Array namesJQUERY Ajax - 使用数组名称传递的 POST 数据
【发布时间】:2015-03-07 23:51:42
【问题描述】:

我有一个表单,输入字段看起来像这样:

<input class="sku_entry" name="sku['901257']['1']" id="sku['901257']['1']" placeholder="Enter Licence" />

<input class="sku_entry" name="sku['901257']['2']" id="sku['901257']['2']" placeholder="Enter Licence" />

<input class="sku_entry" name="sku['901257']['3']" id="sku['901257']['3']" placeholder="Enter Licence" />

每个条目有两个数组名称,一个是产品编号,第二个是计数(订单购物车中的多个项目)我可以将这个直接传递给我的提交脚本,PHP var dump 如下所示:

array(1) { ["sku"]=> array(2) { ["'901257'"]=> array(3) { ["'1'"]=> string(0) "" ["'2'"]=> string(0) "" ["'3'"]=> string(0) "" } ["'901253'"]=> array(2) { ["'1'"]=> string(0) "" ["'2'"]=> string(0) "" } } } 

一切都好 - 我可以处理,不用担心并继续。

问题是我想使用 Jquery AJAX 技术将此数据传递给我的提交脚本。请注意,数组名称总是不同的——所以我不能在 JS 中硬编码任何数组名称。我已经尝试按类定位表单字段,这似乎到目前为止效果最好,但是对于我的生活,我似乎无法让 Jquery 以一种可以通过 PHP 作为数组远程读取的格式将数据传递到我的 PHP 提交脚本.

有什么想法吗??

到目前为止,这是我的 Jquery:

    $('#add_licence_numbers').submit(function(){
    var action = $(this).attr('action');

    $.post(action, {
        // sku: $('#sku').val(), // this works for a targeted field by ID
        // sku: $('.sku').val(), // by classname

        // need to run some sort of loop here to pull in the POST and resend it on to PHP

    },
        function(data)
        {
            $('#add_licence_numbers #submit').attr('disabled','');
            $('.response').remove();
            $('#add_licence_numbers').before('<div class="response">'+data+'</div>');
            $('.response').slideDown();
            if(data=='processed')
            {
                $('#add_licence_numbers').slideUp();
                window.location.href = "http://submissiondomain.com/quote/review/";
            }           
        }
    );
    return false;
}); 

【问题讨论】:

    标签: php jquery arrays ajax post


    【解决方案1】:

    使用serialize() 将表单数据转换为有效的查询字符串:

    $('#add_licence_numbers').submit(function(e){
        e.preventDefault();
        var $form = $(this);
        $.post($form.prop('action'), $form.serialize(), function(data) {
            // rest of your code..
        });
    });
    

    【讨论】:

      【解决方案2】:

      只是做一个序列化?

      $.post( "url" , $("#formID").serialize(), function(data) {});
      

      这将获取您所有的表单数据并以可读格式将其传递回 php。

      【讨论】:

        猜你喜欢
        • 2011-04-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-30
        • 2023-03-23
        • 2015-02-23
        • 2023-03-23
        相关资源
        最近更新 更多