jason_kraft Posted October 16, 2007 Share Posted October 16, 2007 Hi All I need some help with a form operation I am working on. You folks might have come across this many a times. Basically I have an array that fills up a drop down menu in a form. $states_array = array("NC" => "North Carolina", "ND" => "North Dakota", "OH" => "Ohio", "OK" => "Oklahoma", "OR" => "Oregon", "0" => "Other"); Now, on form verification i want to repopulate the drop down menu with the users choice selected so I used the standard foreach $state = $_POST['state']; foreach ($states_arr as $key => $value) { if (isset($state) && ($state === $key)) echo "<option value=\"$key\" selected>$value</option>"; else echo "<option value=\"$key\">$value</option>"; } and here lies the problem $state === $key. When i use "===" I get all the states to match, but the "0" => "Other" does not match. If I use "==" then "0" => "Other" matches but the states don't match and hence won't be selected. I'm assuming this is a type incompatibility. Is there any way around this? Any help would be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/73485-solved-need-help-with-form-operation/ Share on other sites More sharing options...
Lumio Posted October 16, 2007 Share Posted October 16, 2007 try it like that: <?php $state = isset($_POST['state']) ? $_POST['state']:false; foreach ($states_arr as $key => $value) { if ($state == $key) echo "<option value=\"$key\" selected>$value</option>"; else echo "<option value=\"$key\">$value</option>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/73485-solved-need-help-with-form-operation/#findComment-370707 Share on other sites More sharing options...
emehrkay Posted October 16, 2007 Share Posted October 16, 2007 are you sure that the state variable is being set? I dont see any error in your logic.. Try this just for testing purposes $state = $_POST['state']; foreach ($states_arr as $key => $value) { if (isset($state) && ($state === $key)) echo "<option value=\"$key\" selected>$state . ' - '. $value</option>"; else echo "<option value=\"$key\">$state . ' - '. $value</option>"; } Quote Link to comment https://forums.phpfreaks.com/topic/73485-solved-need-help-with-form-operation/#findComment-370715 Share on other sites More sharing options...
kenrbnsn Posted October 16, 2007 Share Posted October 16, 2007 After much debugging of the code, I've discovered that the problem occurs because the index for the "Other" entry in the array is the number zero, not the capital letter "O". Change it to the capital letter and the double equal "==" will work. Here's my code that works: <?php $states_array = array("NC" => "North Carolina", "ND" => "North Dakota", "OH" => "Ohio", "OK" => "Oklahoma", "OR" => "Oregon", "O" => "Other"); ?> <html> <head> <title>Select Test</title> </head> <body> <form method="post"> <select name="state"> <?php $state = (isset($_POST['state']))?$_POST['state']:''; foreach ($states_array as $key => $value) { $sel = ($state == $key && isset($_POST['submit']))?' selected':''; echo '<option value="' . $key . '"' . $sel . '>' . $value . "</option>\n"; } ?> </select><br> <input type="submit" name="submit" value="Test Select"> </form> </body> </html> Ken Quote Link to comment https://forums.phpfreaks.com/topic/73485-solved-need-help-with-form-operation/#findComment-370722 Share on other sites More sharing options...
MadTechie Posted October 16, 2007 Share Posted October 16, 2007 Congrates on the 5000th Post Ken, Quote Link to comment https://forums.phpfreaks.com/topic/73485-solved-need-help-with-form-operation/#findComment-370729 Share on other sites More sharing options...
jason_kraft Posted October 16, 2007 Author Share Posted October 16, 2007 Lumio, emehrkay, The $state variable does register and shows on the page when i tested it. but both methods didn't work :'(. kenrbnsn, That works great. The letter "O" or word "Other" does work, but why not a number? ??? Why doesn't php recognize that. There have already been queries designed and entries inputed through this form by other team members. If I change it to letter "O" there will be a lot of grumbling. Why isn't there a way to compare $state = 0 to $key = 0. (and congrats on the golden century 5000 posts) Quote Link to comment https://forums.phpfreaks.com/topic/73485-solved-need-help-with-form-operation/#findComment-370731 Share on other sites More sharing options...
kenrbnsn Posted October 16, 2007 Share Posted October 16, 2007 I'm still working on why the numeric index doesn't work correctly. When I was doing my testing, I noticed that both the correct index was matching and the numeric index (i.e. both <option> tags were being marked as "selected"). Thanks for the congrats on the 5,000th post. Ken Quote Link to comment https://forums.phpfreaks.com/topic/73485-solved-need-help-with-form-operation/#findComment-370737 Share on other sites More sharing options...
MadTechie Posted October 16, 2007 Share Posted October 16, 2007 simple fix.. hope <?php $states_array = array("NC" => "North Carolina", "ND" => "North Dakota", "OH" => "Ohio", "OK" => "Oklahoma", "OR" => "Oregon", "0" => "Other"); ?> <html> <head> <title>Select Test</title> </head> <body> <form method="post"> <select name="state"> <?php $state = $_POST['state']; foreach ($states_array as $key => $value) { $key = "$key"; if (isset($_POST['state']) && ($state === $key)) echo "<option value=\"$key\" selected>$value</option>"; else echo "<option value=\"$key\">$value</option>"; } ?> </select><br> <input type="submit" name="submit" value="Test Select"> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/73485-solved-need-help-with-form-operation/#findComment-370746 Share on other sites More sharing options...
jason_kraft Posted October 16, 2007 Author Share Posted October 16, 2007 Awesome MadTechie! That just works great. Hey Ken before you pull your hair out too... this is how i used MadTechie's suggestion <?php $state = $_POST['state']; foreach ($states_array as $key => $value) { if (isset($_POST['state']) && ($state === "$key")) echo "<option value=\"$key\" selected>$value</option>"; else echo "<option value=\"$key\">$value</option>"; } ?> It works great. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/73485-solved-need-help-with-form-operation/#findComment-370837 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.