suttercain Posted May 30, 2007 Share Posted May 30, 2007 HI guys, Here is something I have ignored in the past but would like to understand how to do it. Let's say I have a simple form with a dropdown menu for the user to select an item: <?php session_start(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> <body> <form action="dropped.php" method="post" /> <select name="dropdown"> <option value="yes">Yes</option> <option value="no">No</option> <option value="maybe">maybe</option> </select> <input type="submit" name="submit" /> </form> </body> </html> Now i submit the form and have it echoing the selection the user made... simple enough: <?php session_start(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> <?php $_SESSION['dropdown'] = $_POST['dropdown']; echo $_SESSION['dropdown']; ?> <body> </body> </html> So let's say the user selects "maybe", clicks submit and see that "maybe" is echoed to the browser. How do I get it so when the user clicks back in the browser and they are taken back to the form, that "maybe" will be selected in the dropdown menu instead of the defaulted "yes"? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/53612-html-dropdown-in-a-form-how-to-set-it-to-show-value-from-session/ Share on other sites More sharing options...
trq Posted May 30, 2007 Share Posted May 30, 2007 <form action="dropped.php" method="post" /> <select name="dropdown"> <option value="yes"<?php echo $_SESSION['dropdown'] == "yes" ? " selected=\"selected\"" : "" ?>>Yes</option> <option value="no"<?php echo $_SESSION['dropdown'] == "no" ? " selected=\"selected\"" : "" ?>>No</option> <option value="maybe"<?php echo $_SESSION['dropdown'] == "maybe" ? " selected=\"selected\"" : "" ?>>maybe</option> </select> <input type="submit" name="submit" /> </form> Quote Link to comment https://forums.phpfreaks.com/topic/53612-html-dropdown-in-a-form-how-to-set-it-to-show-value-from-session/#findComment-264989 Share on other sites More sharing options...
trq Posted May 30, 2007 Share Posted May 30, 2007 Actually... <form action="dropped.php" method="post" /> <select name="dropdown"> <option value="yes"<?php echo (isset($_SESSION['dropdown']) && $_SESSION['dropdown'] == "yes") ? " selected=\"selected\"" : "" ?>>Yes</option> <option value="no"<?php echo (isset($_SESSION['dropdown']) && $_SESSION['dropdown'] == "no") ? " selected=\"selected\"" : "" ?>>No</option> <option value="maybe"<?php echo (isset($_SESSION['dropdown']) && $_SESSION['dropdown'] == "maybe") ? " selected=\"selected\"" : "" ?>>maybe</option> </select> <input type="submit" name="submit" /> </form> would be safer. Quote Link to comment https://forums.phpfreaks.com/topic/53612-html-dropdown-in-a-form-how-to-set-it-to-show-value-from-session/#findComment-264993 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.