Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

dynamic selection of dropdown list on page load.

Posted on 2014-02-04
4
Medium Priority
?
383 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
[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
  • 2
4 Comments
 
LVL 44

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 44

Accepted Solution

by:
Chris Stanyon earned 2000 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

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

What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Progress

715 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