Getting POST data from a SELECT form dropdown

Can anyone recommend a way to get POST data from a SELECT form dropdown? I have been using PHP to access a particular value from a set of form POST data by doing something like:

$value2 = $_POST[‘value2’]

That works quite nicely to extract the value that I need when the form input box has only a single value . I am now trying to do the same thing when using a select drop down form with multiple options and the same method no longer works. I reference the select name, but no luck. Any suggestions would be appreciated.

if you are using multiple options then make the name of the select an array and all the values will be in there (if i’m not mistaken)

This is what I use for a dropdown menu

<tr> <td class=“form_c”>Salutation </td><td class=“form_c”></td>
<td class=“form_c”>
<select input type =“text” name = “salutation” maxlength = “6” value=“<?php echo $_SESSION[‘salutation’] ?>” ><option></option><option>Mr</option>
<option>Mrs</option>
<option>Ms</option>
<option>Miss</option>
<option>Dr</option>
</select></td>
<td class=“form_c”></td><td class=“form_c”><small>Tips to help fill in the form</small></td></tr>
with this situated on the page you process to…

$salutation = $_POST[‘salutation’];

$_SESSION[‘salutation’] = $salutation;

then I validate the entry…if it is is not valid when I send them back the data is still held, if valid the process move on.

The key thing is the session variable( the selection) held in the session array. Try using this so you can see what is held or not…

		function display_info()

{

echo "<div class=‘info’> ";
echo "<table class=‘sc_tbl’> ";

echo "<tr ><td ><h6 class=‘sc’>Sessions: ";
print_r($_SESSION);
echo "Your session ID is “. session_id().” ";
echo “</h6></td></tr>”;

echo “<tr ><td ><h6 class=‘sc’>GET:”;
print_r($_GET);
echo “Your session GET variables are”;
echo “</h6></td></tr>”;

echo "<tr ><td ><h6 class=‘sc’>POST: ";
echo “Your session POST variables are
\r”;
print_r($_POST);
echo “</h6></td></tr>”;
// here goesthe break
echo "</table> ";
echo "</div> ";

}

just call the function through to the pages you are working on.

I hope that is helpful.

regards

I tried what you suggested jinkas, but it just returns the word “array”. I am doing this for an email form. Here is my code:


<form name="contact" action="mailer.php" method="POST">
<select name="Subject[]">
<option selected value="Products">All Products</option>
<option value="Widgets1">Widgets1</option>
<option value="Widgets2 ">Widgets2</option>
<option value="Widgets3">Widgets3</option>
</select>
<!-- other single input form boxes follow this select  -->
</form>

<?php
$subject = $_POST['Subject[0]'];
echo $subject; // returns "array"
?>

Try this:


  foreach ($_POST['[color=#000000][color=#dd0000]Subject[/color][/color]'] as $key => $value) {
  	echo $value . "<br>";
  	}
  

nonono…

your code is wrong.

<form name=“contact” action=“mailer.php” method=“POST”>
<select name=“Subject”>
<option selected value=“Products”>All Products</option>
<option value=“Widgets1”>Widgets1</option>
<option value="Widgets2 ">Widgets2</option>
<option value=“Widgets3”>Widgets3</option>
</select>
<!-- other single input form boxes follow this select –>
</form>

change this line <select name=“Subject”>

to

<select name=“Subject”>

and you would use

$_POST[Subject] to get your values

Thank you Defiance, that seems to work. The key difference between grabbing something from an input box as opposed to select options seems to be the single quotes.

$_POST[‘Subject’] … works when there is one option coming from an input box
$_POST[Subject] … works when there is one option coming from a multiple choice SELECT dropdown

You use arrays when you need to get values from multiple Select statements for the same entity.

For example, on my current script I have multiple artists per item and on the add item page, the user enters the number of artists they want the item to have and instead of doing <select name=“Artist1”></select><select name=“Artist2”>… <select name=“Artistn”> I simply have <select name=“artist”> for all of them. The same applies for any dynamically created form… it’s much easier just to use an array.

In this case, you only have one select so just give it a name and give each option a value and it will work. $_POST[‘Subject’] is the same as $_POST[Subject] in your case and both will work.