select box problem!!!!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mohawk
Forum Newbie
Posts: 2
Joined: Thu Jan 27, 2005 8:04 pm

select box problem!!!!

Post by mohawk »

I'm trying to display a US state selected from a,listbox when submit is pressed, I can build the listbox and the button, just don't know how to get the value to display when a certain state is selected
here is my code

Code: Select all

<html> 
<head> 
  <title>PHP select box constructor demo</title> 
</head>
<?
include "us_states.php";

function buildSelect($selArr, $selOpt)&#123; 
   
    foreach($selArr as $key => $value)&#123; 
    $sel = ""; 
        for ($loop = 2; $loop < count($selOpt); $loop++) &#123; 
            if ($selOpt&#1111;$loop] == $key) &#123; 
                $sel = "selected"; 
            &#125; 
        &#125; 
        echo "<option $sel value='$key'>$value</option>\n"; 
    &#125; 
    return; 
&#125; 
?>
<body> 
<form method="POST" action=""> 
<select name="allstates" multiple size="6"> 

<? 
	$options = array( "", "", "AL", "CO");
	buildSelect($allstates, $options) 
?> 

</select> 
<input type="submit" name="Submit" value="Click this button to find out what you selected">

<?   //this is where I want to display the state, this is what I'm trying but 
      //my display line does not work, can't display value when something 
     //is selected
if($REQUEST_METHOD == "POST")&#123;
    if($_POST&#1111;$key] != "selected")
	echo "you selected $_POST&#1111;$key] "; 
    else
	// empty
&#125;
?>

</form> 
</body> 
</html>
thanks for any help
mohawk
Forum Newbie
Posts: 2
Joined: Thu Jan 27, 2005 8:04 pm

Post by mohawk »

ps. states are in the "allstates" array that I import from the "us_states.php"

ex. NY => New York
FL => Florida
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you can edit posts... :roll:

Code: Select all

echo $_POST&#1111;'allstates'];
since you have mutliples allowed.. you need to rename the select field allstates[]

then print_r($_POST['allstates']) will show you what was selected.
Post Reply