简体   繁体   中英

Passing a HTML array to PHP through jQuery AJAX

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 - A form with an array, address[], like:

<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...

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

PHP - Data processing and client update

...
 $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:

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

Thanks in advance.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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