Solved

dynamic selection of dropdown list on page load.

Posted on 2014-02-04
4
371 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 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'; ?>

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

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
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 …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

770 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