Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post

    Reproducing chosen <select> option with large lists

    Hey guys... if say I have a <select></select> field with many many options & the users selects an option & submits the form..... now, if there was an error on the submission I am going to reload the form with the error & load all their answers into the fields....

    My question is.... is there a quicker way to make their answer in the select field the selected option (<option value="blah" selected>blah</option>) other than looping through each & every field as they are output on the page to see if that was the answer they selected?

    Cheers for any ideas!

  • #2
    Regular Coder
    Join Date
    Apr 2007
    Posts
    295
    Thanks
    0
    Thanked 19 Times in 19 Posts
    Variable variables

    They're a bit complicated at first, but come in handy every so often

    PHP Code:
    $$_POST['blah'] = 'selected="selected"';

    echo 
    '<select name="blah">';

    echo 
    '<option value="opt_1" '$opt_1 .'>1</option>';
    echo 
    '<option value="opt_2" '$opt_2 .'>2</option>';
    echo 
    '<option value="opt_3" '$opt_3 .'>3</option>';

    echo 
    '</select>'

  • #3
    New Coder
    Join Date
    May 2006
    Posts
    62
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mr e View Post
    Variable variables

    They're a bit complicated at first, but come in handy every so often

    PHP Code:
    $$_POST['blah'] = 'selected="selected"';

    echo 
    '<select name="blah">';

    echo 
    '<option value="opt_1" '$opt_1 .'>1</option>';
    echo 
    '<option value="opt_2" '$opt_2 .'>2</option>';
    echo 
    '<option value="opt_3" '$opt_3 .'>3</option>';

    echo 
    '</select>'
    pretty nifty... very simple.

    This would be susceptible to XSS if Register Globals is turned on however.
    Would also generate E_NOTICE level errors (not really a worry if you're using it on your own server).

    I can't find anything as simple as that, but here is another solution:

    PHP Code:
    $bla = isset($_POST['blah']) ?  $_POST['blah'] : false;

    echo 
    '<select name="blah">';

    echo 
    '<option value="opt_1" '$bla == 'opt_1' ?  'selected="selected"' '' .'>1</option>';
    echo 
    '<option value="opt_2" '$bla == 'opt_2' ?  'selected="selected"' '' .'>2</option>';
    echo 
    '<option value="opt_3" '$bla == 'opt_3' ?  'selected="selected"' '' .'>3</option>';

    echo 
    '</select>'
    Last edited by digital-ether; 06-10-2007 at 01:30 AM.
    Fiji Web Design - where i do Joomla Web Design
    Bucabay.com - My blog

  • #4
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post
    Thanks guys!!

  • #5
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Having PHP throw this many errors could actually be slower.
    You should be looping through all the options anyway if you tore them in the database.
    An alternative would be to have the selected item twice in the select box - as the first option and in the place in which it's supposed to be.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •