How to submit javascript arrays to php

In this tutorial I’m going to show you a little example on how to submit javascript arrays to a php script.

 

Requirements

  • jQuery

 

HTML

First we have to write the html where we are going to input the items that we will put in the arrays.

<table>
<thead>
	<tr>
		<th>Name</th>
		<th>Language</th>
	</tr>
</thead>
<tbody>
	<tr>
		<td><input type="text" class="name_class" name="name[]"/></td>
		<td><input type="text" class="lang_class" name="lang[]"/></td>
	</tr>
	<tr>
		<td><input type="text" class="name_class" name="name[]"/></td>
		<td><input type="text" class="lang_class" name="lang[]"/></td>
	</tr>
	<tr>
		<td><input type="text" class="name_class" name="name[]"/></td>
		<td><input type="text" class="lang_class" name="lang[]"/></td>
	</tr>
</tbody>
</table>

Then the submit button.

<p>
<input type="button" id="btn_submit" value="submit"/>
</p>

Lastly, the <div> where we are going to place the result later on.

<div id="result"></div>

 

Javascript

Next, write the 2 function that will loop through all the contents of the textboxes allotted for the names and the textboxes for the languages. To make things easier, use the jQuery .each() function. The .each() is a function which iterates through  certain elements. You can make use of this to loop through all the contents of the element that you specify. In this case we are looping through the contents of the name_class and lang_class.

function name_loop(){
	var names_array = [];
	$('.name_class').each(function(index){
		names_array[index] = $.trim($(this).val());
	});
	return names_array;
}
	
	
function lang_loop(){
	var language_array = [];
	$('.lang_class').each(function(index){
		language_array[index] = $.trim($(this).val());
	});
	return language_array;
}

Note that you can only specify a single element on a .each() function. That’s the reason why we built 2 functions which basically does the same thing.

Next we need to specify what will happen when the user clicks on the submit button. Were just going to call the 2 functions that we wrote earlier then assign each of them to their own variables. And then we used the jQuery .load() function to call the php script where we are going to pass the arrays and at the same time loads the output from the php script back to the element that we specified, in this case the div with the id of result.

$('#btn_submit').click(function(){
	var names = name_loop(); //stores the array of names
	var languages = lang_loop();  //stores the array of languages
		
	$('#result').load('page.php', {'names' : names, 'languages' : languages}); //submit to php script
	$('input[type=text]').val(''); //empty all textbox
});

Lastly, create another file called page.php. This is the script where you are submitting the javascript arrays. And since we have passed in arrays, we’ll need a loop to actually iterate through all the contents of the array. For this purpose foreach loop is perfect. It’s the same with the jQuery.each() function. But this time were not iterating through element contents, were iterating through the contents of the array.

<table border="1">
<thead>
	<tr>
		<th>Name</th>
		<th>Language</th>
	</tr>
</thead>
<tbody>
	<?php foreach($_POST['names'] as $k=>$v){ ?>
	<tr>
		<td><?php echo $v; ?></td>
		<td><?php echo $_POST['languages'][$k]; ?></td>
	</tr>
	<?php } ?>
</tbody>
</table>

 

Conclusion

That’s it! You’ve learned how to pass javascript arrays into php using AJAX. You also learned how to use the jQuery’s .each() and .load() functions.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s