Validating a select tag

kadin
kadin used Ask the Experts™
on
Can someone tell me why this doesn't work. It is echoing a $value that doesn't make sense. It is echoing "Nothing selected", twice. I don't understand what I am doing wrong. Thanks.

<?php
error_reporting(E_ALL);

$expected = $key = $value = "";

if (isset($_POST['submit'])) {
	$expected = array('cat01','cat02','cat03');
	
	foreach($expected as $key => $value){
        if(isset($_POST['category']) && ($_POST['category'] == $key)) {echo "$value";}
        else {echo "&nbsp;Nothing selected.";}
    }

}
echo "<br>";
print_r($expected);
echo "<br>";
print_r($key);
echo "<br>";
print_r($value);
?>

<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
	<select name="category" size="1">			
		<option value="select_cat" <?php if (!$_POST || $_POST['category'] == 'select_cat') 
		{echo 'selected="selected"';} ?> >Select a category</option>
					
		<option value="cat01" <?php if (isset($_POST['category']) && $_POST['category'] == 'cat01') 
		{echo 'selected="selected"';} ?> >cat01</option>
					
		<option value="cat02" <?php if (isset($_POST['category']) && $_POST['category'] == 'cat02') 
		{echo 'selected="selected"';} ?> >cat02</option>
						
		<option value="cat03" <?php if (isset($_POST['category']) && $_POST['category'] == 'cat03') 
		{echo 'selected="selected"';} ?> >cat03</option>
	</select>
<input name="submit" type="submit" />									
</form>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Multitechnician
Top Expert 2014
Commented:
line 9 : foreach($expected as $key => $value){
$key are index of your array (mean 0,1,2)

and line 10 : ($_POST['category'] == $key)
you compare with cat01,cat02 and cat03

so replace line 10 by :
         if(isset($_POST['category']) && ($_POST['category'] == $value)) {echo "$value";}

Open in new window

Author

Commented:
Thanks very much for your response. Your solution seems to work.
I only have one more question that is baffling me at the moment. I will need just a few minutes to create an image screen shot to show you.

Author

Commented:
I only have one select input tag on my page but I did a screen desktop capture three times and pasted the image on one jpeg. to illustrate the out put of the variables I am echoing or print_r().

Your suggestion works, but the echoing output after the if statement is a mystery to me. It doesn't seem to match.

 
select.jpg

Author

Commented:
It always says

2
cat03
leakim971Multitechnician
Top Expert 2014
Commented:
the first row come from line 10 or 11
the second row (Array...) come from line 16, $expected (always the same, values [x] are the index)
the third one 18 don't change because you start from index 0 to 2 (see my previous comment, $key is the index in the loop)
the last one for the same reason (third line) take the last $value of $expected because you loop over all the three values  

Author

Commented:
Maybe the print_r() is reporting how large the array is.
leakim971Multitechnician
Top Expert 2014

Commented:
no, it display the last value taken by $key and $value
leakim971Multitechnician
Top Expert 2014

Commented:
... in the foreach loop

Author

Commented:
Thanks for you help.

You are really helping me to learn.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial