phatgreenbuds Posted May 17, 2009 Share Posted May 17, 2009 This was originally posted in the javascript forum and it was suggested this is more of a PHP issue so here I am. I am just starting down this path of javascript, seeing where I can enhance a php/mysql page with it. One thing I see right off is the ability to have pages appear to update dynamically without the need to use a submit button. My plan is to try and create a branching form that depending on what options are selected in one section a resultant set of options will appear accordingly in subsequent sections. I have been playing with this little bit of code that I found and have a few questions... <form action="./tester.php" method="GET"> <div align="center""> <select name="state" onchange="this.form.submit();"> <option>Choose One To Submit This Form</option> <option value="CA">CA</option> <option value="TX">TX</option> </select> </div> </form> <?php $test = $_GET['state']; //echo $test; if ($test == "CA") { echo "Ughhh another Liberal"; //this is the first branch where one set of options will appear } else { if ($test == "TX") { echo "You Selected Texas...You ROCK!"; //this is the alternate branch where another set of options will appear } } ?> This works so far and I admit I do not fully understand it yet but the fog is lifting. I'm curious how would I prevent the dropdown from resetting and instead change it to reflect the selected option? And as I start to branch this form further out for more options what would be the best way to preserve the previous selections. I know that somehow I have to change the <option> tag to show "selected" just not sure how to do so. Link to comment https://forums.phpfreaks.com/topic/158518-retaining-selected-values/ Share on other sites More sharing options...
Masna Posted May 17, 2009 Share Posted May 17, 2009 <form action="./tester.php" method="GET"> <div align="center"> <select name="state" onchange="this.form.submit();"> <option>Choose One To Submit This Form</option> <option value="CA"<?PHP echo ($_GET['state'] == "CA")?" selected=\"selected\"":NULL; ?>>CA</option> <option value="TX"<?PHP echo ($_GET['state'] == "TX")?" selected=\"selected\"":NULL; ?>>TX</option> </select> </div> </form> On a side note, there's usually no such thing as an "align" property. Try... <div style="margin-left: auto; margin-right: auto;">CONTENT</div> Link to comment https://forums.phpfreaks.com/topic/158518-retaining-selected-values/#findComment-836061 Share on other sites More sharing options...
phatgreenbuds Posted May 18, 2009 Author Share Posted May 18, 2009 ahhhhh so if I were to take it one step further I could change <option value="CA"<?PHP echo ($_GET['state'] == "CA")?" selected=\"selected\"":NULL; ?>>CA</option> to something like this and simplify the whole thing for say 100 different options <option value="CA"<?PHP echo ($_GET['state'] == $_POST['state'])?" selected=\"selected\"":NULL; ?>>CA</option> Or am I still not getting it? Gonna try and see what happens. BTW thanks for the "align" correction... Link to comment https://forums.phpfreaks.com/topic/158518-retaining-selected-values/#findComment-836089 Share on other sites More sharing options...
phatgreenbuds Posted May 18, 2009 Author Share Posted May 18, 2009 Actually that did not work as I am sure you knew it wouldn't but hey this is my process for learning. You gave me a good start and now with some hacking away at it I am sure I will figure it out. Thanks for the help. Link to comment https://forums.phpfreaks.com/topic/158518-retaining-selected-values/#findComment-836094 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.