Handling array of HTML Form Elements in JavaScript and PHP

Today, I would like to share the way of handling array of HTML FORM elements using JavaScript and PHP.Well, it’s very easy to get the data from the array of HTML form elements in PHP and using them but in JavaScript it’s a bit tricky to handle the array of HTML form elements. I had a hard time to handle them via JavaScript in past thats why I’m posting it here so that people will not have hard time to cope with array of form elements in JavaScript and PHP.

Array of HTML form elements

You can create the array of Form Elements for grouping the similar kind of object or data. The array of elements are very useful in the context where you don’t know how many similar kind of data user have to enter. For example, you have a form where user have to enter his education qualification then you might not know how many of the textboxes are required for a person and in such kind situation you can dynamically generate array of the elements in the form for entering such kind of information.

<input name="education[]" type="text" size="20" maxlength="40" />
<input name="education[]" type="text" size="20" maxlength="40" />
<input name="education[]" type="text" size="20" maxlength="40" />

As you can in the above code, there are array element of textbox defined with the name “education”.Now, let’s see how can we handle them via JavaScript and PHP.

How to handle array of HTML form elements using PHP

If you submit the the the form with the above the array of elements then you can assess it via array of $_POST['education'] in PHP. You can use foreach() loop to access the value of the value of these form elements via PHP.

foreach($_POST['education'] as $key=>$value)
 echo $key.' '.$value;

Normally, posted variable are contained within the POST array but when you post the array of Form Elements then at that time the values are contained within the array of array i.e within $_POST['education'] in above exmaple.

How to handle array of Form elements using JavaScript

Handling the array of Form element part is a bit tricky. Now, let try to access the values of the above elements using JavaScript. First, let’s store the above object in a JavaScript variable

var mutli_education = document.form_name.elements["education[]"];

After storing the object in the variable, we can access the individual variables in the following way in JavaScript

for(i=0;i<mutli_education.length;i++)
{
 alert(mutli_education[i].value);
}

As you can see, you can get how many elements are there in the array using the lengh property and you can use the value property to get the value of the indivisual element.

Popularity: 13% [?]

Enter your email address and get free tutorials, tips and tricks of PHP, Ajax, JavaScript and CSS directly delivered to you email inbox:

Related Posts

» Getting country , city name from IP address in PHP
» Smart contact form for your websites
» Prevent form post request from another domain in PHP
» Cross-site scripting ( xss ) attack by example and prevention in PHP

7 Comments on “Handling array of HTML Form Elements in JavaScript and PHP”

  • Kye Etherton wrote on 2 January, 2009, 11:18

    Thank you very much. Spent 3 days looking for the solution to this problem.

  • IRINEL wrote on 5 March, 2009, 13:48

    Thank you for the article! I found it right on time before jumping over window because of javascript failing methods to acces that checbox array.

  • Raju wrote on 4 May, 2009, 18:07

    Thanks a lot. I have spent a lot of hours on this reading manuals and googling. But I found that it is not working if the array has only one value. The error says that “cannot convert undefined or null to object”. Any solutions ?

    Raju

  • hardyboyz wrote on 8 May, 2009, 3:39

    Hai, nice tutorial. but i had a little problem with another kind in html array. The javascript can’t run if the html form have an array like this :

    What should i do first to get javascript can read those html array??? Please help..

  • sharief wrote on 8 May, 2009, 5:30

    Hi,

    Thanks for providing this. Can you provide some useful tips like using ‘array of HTML Form Elements’ for writing code which can be very flexible in dealing large forms and whose fields may change dynamically.

  • Victor wrote on 28 May, 2009, 8:35

    But, if they have something like this?:

    <input type=”text” name=”data[1][name]” value=”name one”>
    <input type=”text” name=”data[2][name]” value=”name two”>
    <input type=”text” name=”data[3][name]” value=”name three”>

    how can I access it with JavaScript?

  • Mahesh wrote on 8 June, 2009, 3:09

    This is indeed a good article. And thank you so much for it. Just would like to know how this would apply to a checkbox instead of a textbox. For a checkbox, you get all the values of the not just the ones that are checked. How would you address that?

Write a Comment

 


Copyright © 2009 Roshan Bhattarai's Blog. All rights reserved.
Powered by WordPress.org, Custom Theme and ComFi.com Calling Card Company.