dynamic selection of dropdown list on page load.

Posted on 2014-02-04
Last Modified: 2014-02-05
I am working in a php/mysql world.  I am trying to save the contents of a page into a mysql table, and reload the page.

My question had to do with dropdown lists.  

I have lists boxes which are dynamically loaded.  The user makes their selection and the saves the results to a table.

Example:   Lets say I have list which is populated with the following:

   Displayed                          value
    4 inches                               4
    8 inches                               8
   12 inches                              12

The user selects "8 inches" and saves the form.  The table now has "8" stored.

The user now wants to repopulate the form.  Things have changed and the list has  additional selections possible.  The options are as follows:

  Displayed                 value

   2 inches                    2
   4 inches                    4
   6 inches                    6
   8 inches                    8
   12 inches                 12

How do I get the form to display "8 inches"  and have a value of 8?

What if  8 was no longer an option?

Thanks in advance
Question by:jws2bay
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
  • 2
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39835333
When you render your page, you check the value of your variable against the value from the dropdown and add 'selected' to the option. Here's a simple version:

<?php $myValue = '8'; ?>

   <option value='4' <?php echo ($myValue == '4') ? 'selected' : null; ?>>4 Inches</option>
   <option value='8' <?php echo ($myValue == '8') ? 'selected' : null; ?>>8 Inches</option>
   <option value='12' <?php echo ($myValue == '12') ? 'selected' : null; ?>>12 Inches</option>

Open in new window

The echo statement uses a ternary operator : (expression) ? true : false, so if the expression is true, then 'selected' is added to the HTML.

If the option didn't exist, then true would never be fired, so no option would be selected

Author Comment

ID: 39836056
H Chris,

What hapens if the option is no longer available in the list?  All states will be false, so I assume it will come up blank.  Is there a easy way to force this into the option list?

LVL 43

Accepted Solution

Chris Stanyon earned 500 total points
ID: 39836092
Like I said, if the option is no longer there, then it won't be selected, and by default, the first item in the list will be selected, which you could set to something like 'Please Select an Option...' (or just leave it blank)

<?php $myValue = '8'; ?>

   <option value=''>Please Select an Option...</option>
   <option value='4' <?php echo ($myValue == '4') ? 'selected' : null; ?>>4 Inches</option>
   <option value='8' <?php echo ($myValue == '8') ? 'selected' : null; ?>>8 Inches</option>
   <option value='12' <?php echo ($myValue == '12') ? 'selected' : null; ?>>12 Inches</option>

Open in new window


Author Comment

ID: 39836253
In this particular situation I need the page to come up the way it was saved.  I will have to think about what's the best way to handle this.  

 Thanks for the help.

Featured Post

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

734 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