dongtou8736 2015-04-07 17:08
浏览 107

将多个表单数据传递给PHP控制器

$('#save').on("click", function() {
      //collect the form data while iterating over the inputs
      var data = [];
      $('[name=form]').find('input, select, textarea').each(function() {
        if ($(this).attr('value') != '') {
          var name = $(this).attr('name');
          var value = $(this).attr('value');
          data[name] = value;
        }
      })
      for (var i = 1, ii = form.length; i < ii; i++) {
        var name = 'form' + i;
        $('[name=' + name + ']').find('input, textarea').each(function() {
          data[i] = [];
          if ($(this).attr('value') != '') {
            var name = $(this).attr('name');
            var value = $(this).attr('value');
            data[i][name] = value;
          }
        })
      }
      $.post('foo', data,
          function(data) {
            console.log(data);

I have the above jquery code where I wish to post multiple form data into my PHP backend controller.

In my controller, I used

 $input = Input::all()

Question 1

How do I access the data in the array that I have passed? Let's say I have foo input and bar input in my form. I tried to use

$input['foo']
$input['bar']

but I'm getting internal server error with undefined index error.

Question 2

How do I access the data of the data[i][name]? Let's say I have form1, form2, form3, so it my data array will become

$data[1][foo]
$data[2][foo]
$data[3][foo]

How can I access these in my PHP controller?

Thanks and sorry for the long question

  • 写回答

1条回答 默认 最新

  • drl971115 2015-04-07 17:14
    关注

    with FormData object.

    var formdata = new FormData();
    formdata.append("name", value);
    

    on server-side you read values like this:

    $name = $_POST["name"];
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看