how to pull all values from a select box
ok i have this
<select name="testCol[]" id="endresult" size=20>
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
</select>
now i need to pull all these values after someone hits the submit box, i tried this but it doesnt work
foreach($_POST['testCol'] as $key => $value){
echo $key." ---- ". $value ."<br>";
}
what should i do?
Re: how to pull all values from a select box
you can't get all of those values with a select box, unless the user picks all of them, which is impossible. making that <select> an array just makes it possible for you to have multiple <select>s that will share the same array when you submit the form.
to do what you want to do, you would probably have to do something like:
Code:
<select name="testCol">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input type="hidden" name="arrCol[]" value="1" />
<input type="hidden" name="arrCol[]" value="2" />
<input type="hidden" name="arrCol[]" value="3" />
this way, the user's selection ($_POST['testCol'] as a string) and all of the possible values ($_POST['arrCol'] as an array) are all passed. $_POST['arrCol']'s keys will be generated automatically (although if you want specific keys you could do that, too), but all of the possible values will be the same.
Re: how to pull all values from a select box
Sure is possible, but the user would actually have to be able to select multiple.
Code:
<select name="testCol[]" multiple="multiple">
...
</select>
Re: how to pull all values from a select box
Quote:
Originally Posted by kows
you can't get all of those values with a select box, unless the user picks all of them, which is impossible. making that <select> an array just makes it possible for you to have multiple <select>s that will share the same array when you submit the form.
to do what you want to do, you would probably have to do something like:
Code:
<select name="testCol">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input type="hidden" name="arrCol[]" value="1" />
<input type="hidden" name="arrCol[]" value="2" />
<input type="hidden" name="arrCol[]" value="3" />
this way, the user's selection ($_POST['testCol'] as a string) and all of the possible values ($_POST['arrCol'] as an array) are all passed. $_POST['arrCol']'s keys will be generated automatically (although if you want specific keys you could do that, too), but all of the possible values will be the same.
I owe you a +ve -- woops :D
Unless you have it as selected multiple and the user has selected everything, only those selected will appear in your array. Otherwise, you can use kows method. The best way, rather than sending the data to the user and then resending it in the form is to store it server side in a session.