通过jQuery AJAX将HTML数组传递给PHP

时间:2021-03-09 21:29:45

I'm stuck in a probably very common problem when working with HTML forms and jQuery AJAX, but I have not found a proper solution that fits my specific needs yet... I am using Codeigniter Framework. Here is the specific situation:

在处理HTML表单和jQuery AJAX时,我遇到了一个可能非常常见的问题,但我还没有找到适合我特定需求的正确解决方案......我正在使用Codeigniter Framework。具体情况如下:

HTML - A form with an array, address[], like:

HTML - 带有数组,地址[]的表单,如:

<form id="addressForm" class="form-horizontal" method="post" action="">    
<div class="form-group">
    <div class="col-lg-9">
        <label class="control-label" for="address[name]">Full name</label>
        <input name="address[name]" type="text" placeholder="" class="form-control">
    </div>
    <div class="col-lg-3">
        <label class="control-label" for="address[email]">Email</label>
       <input name="address[email]" type="text" placeholder="" class="form-control">
    </div>
    </div>
... and so on

jQuery - AJAX call passing two parameters to PHP: an ID and the address array serialized...

jQuery - AJAX调用将两个参数传递给PHP:一个ID和地址数组序列化...

$.ajax({
    type: "post",
    url: "ajax/updateClientAddress",
    dataType: "json",
    data: {
    id: $('select[name="addresses"]').val(),
        address: $("[name^='address[']").serialize(),
    }
...

PHP - Data processing and client update

PHP - 数据处理和客户端更新

...
 $addressID = $this -> input -> post('id');   // Correctly received
 $addressData = $this -> input -> post('address');
...

I would like to know what is missing or wrong in each part to access the data in PHP like this:

我想知道在PHP中访问数据的每个部分缺少或错误,如下所示:

$client -> name = $addressData['name'];

Thanks in advance.

提前致谢。

1 个解决方案

#1


3  

you need to add the id name/value pair to the address param string and pass that finished param string as the data.

您需要将id名称/值对添加到地址参数字符串,并将完成的参数字符串作为数据传递。

data: $.param({id: $('select[name="addresses"]').val()}) + "&" + $("[name^='address[']").serialize(),

#1


3  

you need to add the id name/value pair to the address param string and pass that finished param string as the data.

您需要将id名称/值对添加到地址参数字符串,并将完成的参数字符串作为数据传递。

data: $.param({id: $('select[name="addresses"]').val()}) + "&" + $("[name^='address[']").serialize(),