I am trying to use PHP to read the $_POST array sent by the following HTML FORM:
Code: Select all
<form name="myform" action="process.php" method="POST">
<select name="Vote100">
<option value="Milk">Fresh Milk</option>
<option value="Cheese">Old Cheese</option>
<option value="Bread">Hot Bread</option>
</select>
<select name="Vote101" multiple="multiple" size="5">
<optgroup label="Swedish Cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
</optgroup>
<optgroup label="German Cars">
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</optgroup>
</select>
</div>
<br><input type="submit" value="Send"><br>
</form>
With help, I've gotten a PHP script that can echo the $_POST values for the "Vote100" option and the "Vote101" optgroup:
Code: Select all
foreach($_POST as $key => $val)
{ echo $key .' is: ' . $val . '<br />';}
echo 'Vote101 is: <br />';
foreach($_POST['Vote101'] as $opt)
{ echo $opt . '<br />';}
However, even though that PHP script snippet works, the echoed result is repetitive.
For example, rather than getting the kind of echoed result I am now getting, as in...
Vote100 is: Cheese
Vote101 is: Array
Vote101 is:
saab
audi
... I am trying to adjust the PHP snippet above so that that same result becomes:
Vote100 is: Cheese
Vote101 is:
saab
audi
In my PHP ignorance, I thought this would work:
Code: Select all
foreach($_POST as $key => $val)
{ if ( $val == 'Array' )
{ foreach($_POST['Vote101'] as $opt)
{ echo $opt . '<br />'; }
} else {
echo '$val is ' .$val . '<br />';
echo $key .' is: ' . $val . '<br />';} }
$val is Cheese
Vote100 is: Cheese
$val is Array
Vote101 is: Array
I had thought that, since $val appears to be returning the word "Array", I could use an if/else conditional to check for when $val == "Array". But I thought wrong, obviously, and then spent a couple of days trying to understand why not and search for a why yes.
Anyway, this is my first post here, so, besides hoping for help, I'm also hoping I've posted a question in the way the forum expects.
Thanks.