Page 1 of 1

determine if $_POST contains a FORM array

Posted: Thu Aug 20, 2009 12:49 am
by niconiko
Newbie PHP who's tried on his own two days (honestly!) but...

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 />';}
 
I learnt this when someone pointed out the use of $_POST with [] to specify an array element.

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 />';} }
 
But the echoed result from that is...

$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.

Re: determine if $_POST contains a FORM array

Posted: Thu Aug 20, 2009 2:13 am
by califdon
Welcome to DevNetwork Forums. Yes, your post is well formed, for which, thank you.

Instead of testing the value of $val, use the php function is_array(). Ref: http://us2.php.net/manual/en/function.is-array.php

Re: determine if $_POST contains a FORM array

Posted: Thu Aug 20, 2009 2:39 am
by niconiko
califdon wrote: Instead of testing the value of $val, use the php function is_array(). Ref: http://us2.php.net/manual/en/function.is-array.php
That did the trick! Thank you, califdon!

In case anyone else is interested (or a knowledgeable someone cares to offer something more elegant), this is what worked:

Code: Select all

 
foreach($_POST as $key => $val)
{
if (is_array($val))
  {
    echo $key .' is ' . '<br />';
    foreach($_POST[$key] as $opt)
    {
    echo $opt . '<br />';
    }
  } else {
   echo $key .' is: ' . $val  . '<br />';
       }
}
 

Re: determine if $_POST contains a FORM array

Posted: Thu Aug 20, 2009 3:15 am
by VladSun