We have seen select element with multiple options selected in previous arrticle. Here I would discuss what happens when we try to submit a form with a selectBox having multiple options selected. Check out the form below.
Check the HTML below :
<body>
Which Bike Models you like?
<form name='f' method='POST'>
<table border='0'>
<tr>
<td>
<select id='selectBox1' size='10' multiple='multiple' style='width:100px' name='select'>
<option value='apr'> Aprilia </option>
<option value='duc'> Ducati </option>
<option value='hro'> Hero </option>
<option value='hnd'> Honda </option>
<option value='hyo'> Hyosung </option>
<option value='kwk'> Kawasaki </option>
<option value='suz'> Suzuki </option>
<option value='yam'> Yamaha </option>
</select>
<br>
<input type='submit' value='submit'>
</td>
</tr>
</table>
</form>
</body>
Notice that the select element has a name 'select' and the form's method is 'post'. Now when we try to submit this, we get only a single value at the PHP server end. The $_POST/$_REQUEST array would look like this :
Array
(
[select] => suz
)
Even if we try to change the form's action method to 'GET', situation hardly improves. The URL of the page would be appended with '?select=hnd&select=suz' and $_REQUEST would still get only the last value 'suz' ignoring all other submitted values.
To get rid of this, we just need to use an array-like name for the select element.
<select id='selectBox1' size='10' multiple='multiple' style='width:100px' name='select[]'>
By using this, we would get all selected options in an array form at the PHP's end. Now the $_POST/$_REQUEST would look like this :
Array
(
[select] => Array
(
[0] => hro
[1] => suz
)
)
Even if we change the form's action method to 'GET', the URL is appended with '?select[]=hnd&select[]=suz' but the $_REQUEST/$_GET still shows the above structure which proves that it gets all values in an array format. Now it becomes very convenient to run a loop and iterate through the array item and work on them.
Check the HTML below :
<body>
Which Bike Models you like?
<form name='f' method='POST'>
<table border='0'>
<tr>
<td>
<select id='selectBox1' size='10' multiple='multiple' style='width:100px' name='select'>
<option value='apr'> Aprilia </option>
<option value='duc'> Ducati </option>
<option value='hro'> Hero </option>
<option value='hnd'> Honda </option>
<option value='hyo'> Hyosung </option>
<option value='kwk'> Kawasaki </option>
<option value='suz'> Suzuki </option>
<option value='yam'> Yamaha </option>
</select>
<br>
<input type='submit' value='submit'>
</td>
</tr>
</table>
</form>
</body>
Notice that the select element has a name 'select' and the form's method is 'post'. Now when we try to submit this, we get only a single value at the PHP server end. The $_POST/$_REQUEST array would look like this :
Array
(
[select] => suz
)
Even if we try to change the form's action method to 'GET', situation hardly improves. The URL of the page would be appended with '?select=hnd&select=suz' and $_REQUEST would still get only the last value 'suz' ignoring all other submitted values.
To get rid of this, we just need to use an array-like name for the select element.
<select id='selectBox1' size='10' multiple='multiple' style='width:100px' name='select[]'>
By using this, we would get all selected options in an array form at the PHP's end. Now the $_POST/$_REQUEST would look like this :
Array
(
[select] => Array
(
[0] => hro
[1] => suz
)
)
Even if we change the form's action method to 'GET', the URL is appended with '?select[]=hnd&select[]=suz' but the $_REQUEST/$_GET still shows the above structure which proves that it gets all values in an array format. Now it becomes very convenient to run a loop and iterate through the array item and work on them.
No comments:
Post a Comment