Solved

dynamic selection of dropdown list on page load.

Posted on 2014-02-04
4
368 Views
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
0
Comment
Question by:jws2bay
  • 2
  • 2
4 Comments
 
LVL 42

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'; ?>

<select>
   <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>
</select>

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
0
 

Author Comment

by:jws2bay
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?

Thanks
0
 
LVL 42

Accepted Solution

by:
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'; ?>

<select>
   <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>
</select>

Open in new window

0
 

Author Comment

by:jws2bay
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.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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.
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video teaches users how to migrate an existing Wordpress website to a new domain.

707 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

12 Experts available now in Live!

Get 1:1 Help Now