?
Solved

Insert 'selected="selected" ' into HTML of Option List in Page Populated by MySQL, to Indicate Which Option Was Previously Selected, According to the Value Currently Stored in MySQL

Posted on 2007-03-25
3
Medium Priority
?
165 Views
Last Modified: 2013-12-13
In an edit/update form to update an existing record in MySQL, the form inputs are populated from the database.  How do I code it to show which option was already selected in a dropdown option box?  

For example, if the select box html is:

<select name="Comedian">
      <option value="">Select Comedian</option>
      <option value="Curly">Curly</option>
      <option value="Larry">Larry</option>
      <option value="Moe">Moe</option>
</select>

and the the $Comedian variable from MySQL = "Larry", how do I make the list echo like:

      <option value="">Select Comedian</option>
      <option value="Curly">Curly</option>
      <option selected="selected" value="Larry">Larry</option>
      <option value="Moe">Moe</option>

where Larry's row contains   selected="selected" ?

Note: I'm not currently populating the entire list of options from MySQL; they're hard-coded into the html form and will not need to change.  The list is currently in a plain-HTML portion of the page, and any php echos in that part are currently like <?php echo $var; ?> .  Is there a way to make php insert the  'selected="selected" ' in the right row of the html list without having to populate the entire list from MySQL?
0
Comment
Question by:Randall-B
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 11

Accepted Solution

by:
JamesCssl earned 1000 total points
ID: 18789496
You could hard code that as well:

<select name="Comedian">
      <option value="">Select Comedian</option>
      <option value="Curly" <?php echo ($Comedian=='Curly')?'selected="selected"':'';?>>Curly</option>
      <option value="Larry" <?php echo ($Comedian=='Larry')?'selected="selected"':'';?>>Larry</option>
      <option value="Moe" <?php echo ($Comedian=='Moe')?'selected="selected"':'';?>>Moe</option>
</select>
0
 
LVL 11

Expert Comment

by:JamesCssl
ID: 18789525
On my site, I did something like the following:

<?php
function contactEntity($entityName, $formalName, $entityEmail) {
      global $contactOptions, $userRecipient;
      $contactOptions .= '<option value="' . $entityName . '" ' .
            (($userRecipient==$entityName)?'selected="selected"':'') .'>' . $formalName . '</option>';
}


      # start list of possible recipients
      contactEntity('person1','First1 Last1','email1@example.com');
      contactEntity('person2','First2 Last2','email2@example.com');
      contactEntity('person3','First3 Last3','email3@example.com');
      echo $contactOptions;
?>

where $userRecipient holds the value of the selected option, and the string of options is built by multiple calls to the function
            
0
 

Author Comment

by:Randall-B
ID: 18789535
Great. Your first response is exactly what I was looking for, and I may look into using the format from your second comment. Thanks.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question