garyed Posted March 14, 2010 Share Posted March 14, 2010 Is it possible to keep the selected drop down box choice on the screen after submitting it on a form? Here's a sample code, if I choose medium the correct number will be shown but the selection will immediately return to high. I don't have this problem in javascript but I'd like to code this in php but i have a lot of drop down boxes. <html> <head></head> <body> <form method="get" action="<?php echo $_SERVER['PHP_SELF'] ?>"> <select name="rpms"> <option name="high" value="1200"> high</option> <option name=medium value="1000">medium</option> <option name="low" value="825"> low </option> <br> <input type="submit" value="Submit"> <?php $speed = $_GET[rpms]; ?> </form> <div id="new_speed"> <?php echo ($speed); ?> </div> </body></html> Quote Link to comment https://forums.phpfreaks.com/topic/195161-drop-down-box-question/ Share on other sites More sharing options...
ocpaul20 Posted March 14, 2010 Share Posted March 14, 2010 isn't there a 'selected' attribute or something that can be set? Remember that everything has normally been done to the page once you have processed your PHP code, so the second time through the program (after you have submitted the form) you will probably have to change the option code to put(add) the selected attribute against the one they selected on the first time through. Does that make sense? Quote Link to comment https://forums.phpfreaks.com/topic/195161-drop-down-box-question/#findComment-1025786 Share on other sites More sharing options...
teamatomic Posted March 14, 2010 Share Posted March 14, 2010 Easier to show it than explain what I mean for this. <html> <head></head> <body> <?php $speed = $_GET[rpms]; $drop=array( '1200' =>'<option name="high" value="1200"> high</option> <option name=medium value="1000">medium</option> <option name="low" value="825"> low </option>', '1000' => '<option name=medium value="1000">medium</option> <option name="low" value="825"> low </option> <option name="high" value="1200"> high</option>', '825' =>'<option name="low" value="825"> low </option> <option name=medium value="1000">medium</option> <option name="high" value="1200"> high</option>' ); ?> <form method="get" action="<?php echo $_SERVER['PHP_SELF'] ?>"> <select name="rpms"> <?php if(isset($_GET['rpms'])) {echo "{$drop[$speed]}";} else{ echo '<option name="high" value="1200"> high</option> <option name=medium value="1000">medium</option> <option name="low" value="825"> low </option>'; } ?> <br> <input type="submit" value="Submit"> </form> <div id="new_speed"> <?php echo ($speed); ?> </div> </body></html> HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/195161-drop-down-box-question/#findComment-1025787 Share on other sites More sharing options...
garyed Posted March 14, 2010 Author Share Posted March 14, 2010 Wow, thanks Teamatomic That works perfect, I almost understand it but I'm not sure what the " => " means in the array. I see that the order of the boxes follow the numbers in the array. I'm obviously not too sharp on this stuff yet. Quote Link to comment https://forums.phpfreaks.com/topic/195161-drop-down-box-question/#findComment-1025799 Share on other sites More sharing options...
garyed Posted March 14, 2010 Author Share Posted March 14, 2010 Well I should have stated that I have about 10 drop boxes each with about 25 choices on the form I'm working on so that might be a little impractical to code it that way. It looks like I might have to stick to javascript for this one. Let's see if I understand it right; php works from the server so after a form is submitted & it does its thing with the variables it reloads the page & that is why the drop boxes will go back to their default settings. It sounds like that would be an interesting function to figure out but its definitely beyond me, at least for now. Quote Link to comment https://forums.phpfreaks.com/topic/195161-drop-down-box-question/#findComment-1025984 Share on other sites More sharing options...
teamatomic Posted March 15, 2010 Share Posted March 15, 2010 I did that to show you how it works. You would only need to build an array for each group once. Then to display it use the Submited as a key, just as we do now. the but is that you would then build the array like this $select=array( 'rpms' =>array( '1200' => '<option name="high" value="1200"> high</option>', '1000' => '<option name=medium value="1000">medium</option>', '825' => '<option name="low" value="825"> low </option>'), 'speed trap' = array( 'Car 1' => ' 3rd and Main St', 'Car 2' => 'Eight Mile and Gratiot', 'Car 2' => 'Red Light District') ); Then build the selects echo "{$select[$group][$rpms]"; foreach($select[$group] as $key => $value) { if($key != $rpms){echo "$value";} } It sounds a bit complicated and it sorta is. But... this is a good time to use a variable variable. study this. Put it in a test page and call it to see what group and rpms output. $group='rpms'; $$group='1000'; echo "$group"; echo '<br>'; echo "$rpms"; it will suit the selection of the proper group and the value associated with it quit well. HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/195161-drop-down-box-question/#findComment-1026091 Share on other sites More sharing options...
garyed Posted March 16, 2010 Author Share Posted March 16, 2010 Thanks again, I'll study the code until I understand it. I really appreciate the help. Gary Quote Link to comment https://forums.phpfreaks.com/topic/195161-drop-down-box-question/#findComment-1026737 Share on other sites More sharing options...
garyed Posted March 16, 2010 Author Share Posted March 16, 2010 I must be missing something because I getting errors when I try to use the code in a php page. Quote Link to comment https://forums.phpfreaks.com/topic/195161-drop-down-box-question/#findComment-1026758 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.