Page 1 of 1

each(_POST)

Posted: Sat Sep 07, 2002 11:03 pm
by mdunnOutlaw
Perhaps someone has already come up with a solution for this, however, I've written a very nice function that goes through any form and checks all required fields. However, if I use input type="Radio" or input type="Checkbox", the each() does not pick up on these form fields. Is there a better way way of doing this?

I've tried the following two ways:

while(list($key, $val) = each($_POST)) {
array_push($formKeys, $key);
array_push($formVals, $val);
}

or:

$formKeys = array_keys($_POST);
$formVals = array_values($_POST);

both ways ignore the radio and checkbox fields, what am I missing? I haven't tried to use multiple select boxes, though selects work just fine.

Answer to my own question :)

Posted: Sun Sep 08, 2002 12:10 am
by mdunnOutlaw
If a checkbox or radio button is not selected in the form then it does not come through. The each(_POST) works fine as long as a value is selected, however, if it is not then you have to do a specific check for that field. If it does not exists in the _POST array of keys then you know that nothing was selected and if it is a required field then you can send them back. :D

Posted: Sun Sep 08, 2002 12:53 am
by Takuma
Have you set the "name" property with check-box form?

Code: Select all

<input type="checkbox" name="SOMETHING">

RE: each(_POST)

Posted: Sun Sep 08, 2002 2:18 am
by mdunnOutlaw
Yes I did set the name. The radio elements look as follows:

Code: Select all

<input type="Radio" name="pro" value="Yes">
<input type="Radio" name="pro" value="No">
What I found was that since neither is selected/checked and they remain so when the form is submitted then they do not come across in the list of keys for the each(_POST). It's not a PHP issue it's just how IE and probably Netscape (haven't tried it with Netscape), handles those particular form elements when nothing is selected. I've run into the same issues with other languages; I'm really not sure why it didn't occur to me to begin with. Thanks for the input though :).