I saw something on this site:
Handling array of HTML Form Elements in JavaScript and PHP http://www.ajaxprojects.com/ajax/tutorialdetails.php?itemid=343
It said to put the array in the name
property and how to get the input collection's value. For example, name="education[]"
But as I know, an HTML input element is array-ready by name
. On the client-side (GetElementsByName
) or server-side ($_POST
in PHP or Request.Form
in ASP.NET).
For example: name="education"
, so what is the different with or without the []
?
PHP uses the square bracket syntax to convert form inputs into an array, so when you use name="education[]"
you will get an array when you do this:
$educationValues = $_POST['education']; // Returns an array print_r($educationValues); // Shows you all the values in the array
So for example:
<p><label>Please enter your most recent education<br> <input type="text" name="education[]"> </p> <p><label>Please enter any previous education<br> <input type="text" name="education[]"> </p> <p><label>Please enter any previous education<br> <input type="text" name="education[]"> </p>
Will give you all entered values inside of the $_POST['education']
array.
In JavaScript, it is more efficient to get the element by id...
document.getElementById("education1");
The id doesn't have to match the name:
<p><label>Please enter your most recent education<br> <input type="text" name="education[]" id="education1"> </p>
If you have checkboxes, you can pass an array of checked values.
<input type="checkbox" name="fruits[]" value="orange"/> <input type="checkbox" name="fruits[]" value="apple"/> <input type="checkbox" name="fruits[]" value="banana"/>
Also multiple select dropdowns
<select name="fruits[]" multiple> <option>apple</option> <option>orange</option> <option>pear</option> </select>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With