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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

863 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now