dongzhuo8210 2019-04-16 14:48
浏览 54

使用ajaxForm插件发布数组

I'm using this plugin --> http://jquery.malsup.com/form/

But I can't post an array to a PHP script.

I tried this in the beforeSubmit function:

var concepts = new Array();
$('#myTable tbody tr td span.file').each(function(){
  concepts.push($(this).text());
});
arr.push({name: 'concepts',value:concepts});

And this in the data parameter:

data: {
  concepts: function(){
    var concepts = new Array();
    $('#myTable tbody tr td span.file').each(function(){
      concepts.push($(this).text());
    });
    return concepts;
  }
}

But I always receive a comma separated string in the PHP script not an array like in any ajax call.

[concepts] => 20 Bafles J8 DB Line Array, 4 Bafles J12 DB Line Array, 10 Bafles J SUB, 12 Bafles B2 DB, 2 Bummpers J, 4 Bafles Q7 DB Front Fill, 12 Monitores M4 DB, 38 Amplificadores, Sub Snake, 6 Bafles Q1 DB Side Fill, 6 Bafles Q SUB DB, Centro de Carga,Andamio de 4 mts. de altura brandeados por los 4 lados con lona mesh a 1,200 dpis

The above is the result of:

print_r($_POST)

The array has 2 elements in this example.

But my posted data has commas. I know that I can encode to JSON and then decode inside PHP script but, I prefer to send and receive an array. Is there any way to achieve this?

I tried using processData as false, and nothing :(

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • douzhao7445 2019-04-16 17:10
    关注

    In your example for the beforeSubmit function you seem to be pushing multiple values under the same input name without declaring it as an array (if that make sense). See the updated example:

    var concepts = new Array();
    $('#myTable tbody tr td span.file').each(function(){
      concepts.push($(this).text());
    });
    
    // Added square brackets to the `name` attribute
    arr.push({name: 'concepts[]',value:concepts});
    

    If that doesn't work, I would try sending the data to the server without the ajaxForm() plugin. For example:

    $('form').on('submit', function() {
        var data = $(this).serializeArray();
    
      $('#myTable tbody tr td span.file').each(function(){
        data.push({name: 'concepts[]', value: $(this).text()});
      });
    
      $.ajax({
        url: '/post/endpoint/example', // Change me!
        method: 'post',
        data: data
      });
    
      return false;
    
    });
    

    If posting without the plugin works then I would assume the plugin is trying to be overly clever and doing some weird serializing to the form data.

    评论

报告相同问题?

悬赏问题

  • ¥15 用三极管设计—个共射极放大电路
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示