Jump to content

"selected" dropdown menu option


aebstract

Recommended Posts

Okay, I know for a fact there are short-hand ways of doing what I am trying to accomplish, I'm just not 100% sure on the methods. Here is what I have for the code for a drop down menu:

 

<select name=\"dropdown2\">";
  $result = mysql_query("SELECT * FROM materials WHERE type='sheet' ORDER BY material ASC") or DIE(mysql_error());
  while($r=mysql_fetch_array($result)){

$mid=$r["id"];
    $material=$r["material"];
    $price=$r["price"];
    $width=$r["width"];
    $length=$r["length"];

  $content .= "<option value=\"{$mid}\">{$material}</option>\n";

}

$content .= "</select>

 

Somehow I would like it so that when I submit this form and have my $_POST['dropdown2'] variable, I can set the correct option as selected that matches which was selected on submit. Is there some short code I can throw in to my option field?

Link to comment
https://forums.phpfreaks.com/topic/101714-selected-dropdown-menu-option/
Share on other sites

<?php
if(isset($_POST['dropdown2'])){
   if($mid = $_POST['dropdown2']){
      $content .= "<option value=\"{$mid}\" selected=\"selected\">{$material}</option>\n";
   } else{
      $content .= "<option value=\"{$mid}\">{$material}</option>\n";
   }
}
?>

 

Hope it is what u wanted.

The code he gave you was entirely fine (with the exception of the if statement, it was missing an equals sign). But if you want to be really picky, you can use this:

 

<?php
if(isset($_POST['dropdown2'])){
   if($r['id'] == $_POST['dropdown2']){
      $content .= "<option value=\"{$r['id']}\" selected=\"selected\">{$r['material']}</option>". PHP_EOL;
   } else{
      $content .= "<option value=\"{$r['id']}\">{$r['material']}</option>" . PHP_EOL;
   }
}
?>

Unless one of the wizzes jumps in and makes a note on this, I'll prob. end up using an if statement such as this. Though I know for a fact it can be done in one short line of code. I had a navigation set up once that it would decide which menu item was chosen by a single line of code, but I forget how I had it and I don't think I have the file anymore.

Look big guy, you have two people who gave you well formed code that was better than yours, and you come up with 'unless one of the wizzes hops in'. Not a thanks at all. Then you say 'I know for a fact it can be done'. Why aren't you doing it yourself then? I'm guessing its because you are a shatty coder.

 

$content .=  ($r['id'] == $_POST['dropdown2']) ? "<option value=\"{$r['id']}\" selected=\"selected\">{$r['material']}</option>". PHP_EOL : "<option value=\"{$r['id']}\">{$r['material']}</option>" . PHP_EOL;

 

This code is a hell of a lot sloppier as its hard to read. But its what you wanted. Now kindly go and screw off.

Dude seriously, you're missing out on what I'm saying. I said that I would probably end up using your methods and that should be a thanks in itself, but I'm sorry that you have to have it handed to you on a silver platter to be happy with yourself and I am guessing your self-esteem. My code doesn't have this in it, cause I hadn't attempted yet.

 

Then you say 'I know for a fact it can be done'. Why aren't you doing it yourself then? I'm guessing its because you are a shatty coder.

I have mentioned several times that I did not know what had to be done and that I do know it can be. That is the reason I am not doing it myself. Please read.

Though thanks for the name calling, kid. Jeez 0.o

 

It's not done directly through an if statement the way you are trying, which is why I was hoping maybe someone else would come in that knew how to do it the way I was getting at. If you don't, stop trying then?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.