0

I have multiple input fields like this:

<input type="text" name="childage[3]" placeholder="0"/>
<input type="text" name="childage[8]" placeholder="0"/>
<input type="text" name="childage[12]" placeholder="0"/>

I want to get them into jQuery and pass them with AJAX to php but remember the keys (3, 8, 12). Is this possible?

I tried until now the following:

$('input[name="childage[]"]');
$('input[name="childage"');

2 Answers 2

3

You should have a look at the serialize method:

http://docs.jquery.com/Ajax/serialize

You'll want to grab all the form elements, something like this:

<input class="age-box" type="text" name="childage[3]" placeholder="0"/>
<input class="age-box" type="text" name="childage[8]" placeholder="0"/>
<input class="age-box" type="text" name="childage[12]" placeholder="0"/>

var ages = $('.age-box').serialize();
$.post(url, ages, function(data) { //handle response from the server });
Sign up to request clarification or add additional context in comments.

2 Comments

Note that serialize will replace [ and ] for url encoding. You will have to replace them back $('.age-box').serialize().replace(/%5B/g, '[').replace(/%5D/g, ']'); or with something similar.
Thank You! In php i used parse_str($_POST['fields'], $fields); and it works
0

You could use hidden input fields and set the values to the numbers you need:

<input type="hidden" value=3 />
<input type="hidden" value=8 />
<input type="hidden" value=12 />

// Get the values
$('input[type="hidden"]')[0].val();
$('input[type="hidden"]')[1].val();
$('input[type="hidden"]')[2].val();

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.