4

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.

0

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

data: $.param({id: $('select[name="addresses"]').val()}) + "&" + $("[name^='address[']").serialize(),
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.