Jump to content

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/54873-selected-value-in-drop-down-menu/
Share on other sites

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.