简体   繁体   English

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

[英]Passing multiple form data to PHP controller

$('#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. 我有上面的jquery代码,希望将多个表单数据发布到我的PHP后端控制器中。

In my controller, I used 在我的控制器中,我使用了

 $input = Input::all()

Question 1 问题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. 假设我在表单中有foo输入和bar输入。 I tried to use 我尝试使用

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

but I'm getting internal server error with undefined index error. 但我收到带有未定义索引错误的内部服务器错误。

Question 2 问题2

How do I access the data of the data[i][name]? 如何访问数据[i] [名称]的数据? Let's say I have form1, form2, form3, so it my data array will become 假设我有form1,form2,form3,所以我的数据数组将变为

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

How can I access these in my PHP controller? 如何在我的PHP控制器中访问它们?

Thanks and sorry for the long question 谢谢你,还有很长的问题很抱歉

with FormData object. 与FormData对象。

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

on server-side you read values like this: 在服务器端,您读取的值是这样的:

$name = $_POST["name"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM