Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

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

data: $.param({id: $('select[name="addresses"]').val()}) + "&" + $("[name^='address[']").serialize(),
share|improve this answer
    
That was exactly what I needed. Thank you very much. –  andcl85 Jan 14 at 10:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.