JPark Posted July 24, 2009 Share Posted July 24, 2009 It is driving me crazy! I have a simple array of the states as such: $states = array(); $states['AL'] = "Alabama"; $states['AK'] = "Alaska"; $states['AZ'] = "Arizona"; $states['AR'] = "Arkansas"; $states['CA'] = "California"; $states['CO'] = "Colorado"; $states['CT'] = "Connecticut"; etc from which I make a simple list/form: <form name="myform" method="post" action="test.php"> <select name="states[]" size="10" multiple="yes"> <? foreach ($states as $key=>$value) { echo "<option name='".$key."'>".$value."</option>"; } ?> </select> <input type="submit" value="submit" /> <input name="reset" type="reset" id="reset" value="Clear Form" /> </form> When the selections are sent to "test.php", I display their choices: $states=$_POST['states']; $sectors=$_POST['sectors']; if (is_array($states) && isset($states) ) { echo "<b>You have selected the following state(s):</b><br><blockquote>"; foreach($_POST['states'] as $v) { if( in_array($v, $states, TRUE) ) { echo $v."<br>"; } } echo "</blockquote>"; } else { echo "<p class='notice'>You have not selected any states.</p>"; } So far, so good... But, I want to display the picked states AND (in another spot on the page) display the abbreviations for the picked states. How? It seems that I can only send to "test.php" the state name OR its abbreviation but not both. Help?? Quote Link to comment Share on other sites More sharing options...
btherl Posted July 24, 2009 Share Posted July 24, 2009 What if you send the abbreviation only, then use it to look up the long form in the original array you used to generate the form? Quote Link to comment Share on other sites More sharing options...
Andy-H Posted July 24, 2009 Share Posted July 24, 2009 The name of the postdata is defined in the <select> tag, you should be using echo "<option value=\"" . $key . "\">" . $value . "</option>\n"; // edit You could actually use: echo "<option value=\"" . $key . "-" . $value . "\">" . $value . "</option>\n"; Then in your foreach loop explode the posted value ($v) with the dilemiter "-" nd end up with an array of both the abbreviation and full state name. Quote Link to comment Share on other sites More sharing options...
JPark Posted July 24, 2009 Author Share Posted July 24, 2009 See??? I TOLD you it was easy. echo "<b>These are the state abbreviations:</b><br>"; foreach ($_POST['states'] as $temp) { $state_chunks=explode("-",$temp); echo $state_chunks[0]."<br />"; } if (is_array($states) && isset($states) ) { echo "<b>You have selected the following state(s):</b><br><blockquote>"; foreach($_POST['states'] as $v) { if( in_array($v, $states, TRUE) ) { echo $v."<br>"; } } echo "</blockquote>"; } Much thanks, Andy! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.