chicabella Posted June 9, 2007 Share Posted June 9, 2007 I want the first option ("selected" option) in a drop down menu to be based on the URL a user clicks on. For example, if the user clicks on the link: layouts.php?submit=true&category=abstract The first and selected value in the drop down menu should be abstract Then the rest of the menu will pull from the database. The code I am using now (below) lists all my categories from my database, but it is not putting the selected value first. How can I get this to work?? <select name="category"> <option value="all">All</option> <? $query = mysql_query("SELECT * FROM `categories`"); while ($result = mysql_fetch_assoc($query)) { if ($row["category"] == $_POST['category']) { echo '<option value="'.$result["id"].'" SELECTED>'.$result["name"].'</option>'; } else { echo '<option value="'.$result["id"].'">'.$result["name"].'</option>'; } } ?> </select> Quote Link to comment https://forums.phpfreaks.com/topic/54873-selected-value-in-drop-down-menu/ Share on other sites More sharing options...
chocopi Posted June 9, 2007 Share Posted June 9, 2007 Use GET <?php $cat = $_GET['category']; echo "<select name=\"category\">"; echo "<option value=\"".$cat."\" selected=\"selected\">".$cat."</option>"; echo "<option value=\"all\">All</option>"; echo "<option value=\"all\">All</option>"; $query = mysql_query("SELECT * FROM `categories`"); while ($result = mysql_fetch_assoc($query)) { if ($row["category"] == $_POST['category']) { echo '<option value="'.$result["id"].'">'.$result["name"].'</option>'; } else { echo '<option value="'.$result["id"].'">'.$result["name"].'</option>'; } } </select> ?> I hope it helps, ~ Chocopi Quote Link to comment https://forums.phpfreaks.com/topic/54873-selected-value-in-drop-down-menu/#findComment-271394 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.