synking Posted August 12, 2009 Share Posted August 12, 2009 hey guys i have a select box that i create with an array foreach loop. it is in a form where you can select different options but if you forget to fill something out it take you back to the form with the options filled out. I am having an issue getting the select box to default to what they already had selected. here is my code. <select name="state" id="dial-up search" tabindex="6" size="1"> <?php foreach ($dailsearch as $key => $value) { if($value == $_POST['state']) { echo ("<option value=\"'$state' selected\">$value</option>\n"); } else { echo ("<option value=\"$key\">$value</option>\n"); } ?> </select> But it does not seem to work it always reprints the entire option list. if ya can even just point me in the right direction it would be great. Quote Link to comment https://forums.phpfreaks.com/topic/169953-solved-foearch-created-select-box-have-post-default-selected/ Share on other sites More sharing options...
WolfRage Posted August 12, 2009 Share Posted August 12, 2009 Is this for html or xhtml and which version number? Quote Link to comment https://forums.phpfreaks.com/topic/169953-solved-foearch-created-select-box-have-post-default-selected/#findComment-896573 Share on other sites More sharing options...
synking Posted August 12, 2009 Author Share Posted August 12, 2009 it is HTML and i have been shooting for html4 is something that can only be done in xhtml or something. Quote Link to comment https://forums.phpfreaks.com/topic/169953-solved-foearch-created-select-box-have-post-default-selected/#findComment-896575 Share on other sites More sharing options...
smerny Posted August 12, 2009 Share Posted August 12, 2009 whats with the ( ) around what you are echoing? probably doesn't do anything i guess.. Quote Link to comment https://forums.phpfreaks.com/topic/169953-solved-foearch-created-select-box-have-post-default-selected/#findComment-896577 Share on other sites More sharing options...
shadowfiend20 Posted August 12, 2009 Share Posted August 12, 2009 check the spelling of DIAL in foreach loop..i cud figure it out this only nthing much apart frm this Quote Link to comment https://forums.phpfreaks.com/topic/169953-solved-foearch-created-select-box-have-post-default-selected/#findComment-896580 Share on other sites More sharing options...
synking Posted August 12, 2009 Author Share Posted August 12, 2009 the spelling was just a typo here. so no issue there but it is still not working. prints the whole option list. not just the previous selected like i want it to. edit i just checked it again and now it does not print anything and i get this error. <b>Warning</b>: Invalid argument supplied for foreach() in in the HTML source not printed to the screen Quote Link to comment https://forums.phpfreaks.com/topic/169953-solved-foearch-created-select-box-have-post-default-selected/#findComment-896740 Share on other sites More sharing options...
synking Posted August 13, 2009 Author Share Posted August 13, 2009 I found out what the issue is when doing a foreach if statement you don't need to add the else clause it will pass through each key and when it hits the one you want it will change the info. but thanks for all the help. Quote Link to comment https://forums.phpfreaks.com/topic/169953-solved-foearch-created-select-box-have-post-default-selected/#findComment-897338 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.