test
test

Reputation: 18198

Inserting into database via JavaScript AJAX requests

Let's say I have a form that has multiple inputs. Like an array... so we'd have:

<input name='firstname[]' type='text' value='' /> <input name='lastname[]' type='text' value='' /> <input name='dob[]' type='text' value='' /> 
<input name='firstname[]' type='text' value='' /> <input name='lastname[]' type='text' value='' /> <input name='dob[]' type='text' value='' /> 
<input name='firstname[]' type='text' value='' /> <input name='lastname[]' type='text' value='' /> <input name='dob[]' type='text' value='' /> 
<input name='firstname[]' type='text' value='' /> <input name='lastname[]' type='text' value='' /> <input name='dob[]' type='text' value='' />

Imagine instead of 4, there is like 50. OK, so the way I do it right now is through a regular HTML form submission using PHP like so:

foreach ($_POST['firstname'] as $fname) {
...
}

Anyway, would it be better to do it through a JavaScript foreach event where each submission is submitted or do one simple AJAX request then after that one is finished.. go to the next one until the submissions is done.

Upvotes: 0

Views: 49

Answers (2)

Madhu
Madhu

Reputation: 2551

Serialize the Form elements and send as JSON to the other side.

var data = $("form").serialize()

$.ajax({ ... data:data .....

})

Upvotes: 1

Quentin
Quentin

Reputation: 943579

HTTP requests are (in relative terms) very time consuming.

Send all the data in one go, just as you do with a regular form submission.

Upvotes: 4

Related Questions