SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Wizard bronze trophy conradical's Avatar
    Join Date
    Jan 2005
    Location
    Atlanta
    Posts
    2,354
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)

    Maintaining the Select option when validating a form

    I have a form with a bunch of <select> fields - most of these have only about 4 <options>. I am first writing the PHP validation and this checks if the user has selected an <option> then it adds the attribute of "selected" to that option. However there are a lot of if statements (the way I am doing it). I have a state field with 50 of the US states and I am wary about adding 50 unnecessary if statements. Guidance is appreciated.

    Here's what I have:
    Code PHP:
    // Validation
    if(trim($_POST['state']) === '') {
                $stateError = 'Please select your state.';
                $hasError = true;
            } else {
                $state = trim($_POST['state']);
            }


    Code PHP:
    <option value="Alaska" <?php if($state=="Alaska") echo(" selected=\"selected\"");?>>Alaska</option>

    First off, writing all those statements for each state would be a pain, second I am not sure about all those if statements. How would a pro handle this? Teach me!

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    Declare an array of states.
    Foreach States as State
    Echo Option, if State === Selected, echo selected="selected"

    Programming rule of thumb: If you're going to do something more than 3 times, reduce it. In this case, 50 Options becomes 4 lines of code.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    SitePoint Zealot
    Join Date
    Feb 2005
    Location
    Aveiro, Portugal
    Posts
    164
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by conradical View Post
    I have a form with a bunch of <select> fields - most of these have only about 4 <options>. I am first writing the PHP validation and this checks if the user has selected an <option> then it adds the attribute of "selected" to that option. However there are a lot of if statements (the way I am doing it). I have a state field with 50 of the US states and I am wary about adding 50 unnecessary if statements. Guidance is appreciated.

    Here's what I have:
    Code PHP:
    // Validation
    if(trim($_POST['state']) === '') {
                $stateError = 'Please select your state.';
                $hasError = true;
            } else {
                $state = trim($_POST['state']);
            }


    Code PHP:
    <option value="Alaska" <?php if($state=="Alaska") echo(" selected=\"selected\"");?>>Alaska</option>

    First off, writing all those statements for each state would be a pain, second I am not sure about all those if statements. How would a pro handle this? Teach me!
    Something like:
    <option value="Alaska" <?php ($state==trim($_POST['state'])) ? echo("selected=\"selected\"") : '';?>>Alaska</option>

    would solve right away.

    HTH
    Yours truely
    Mário Ramos

  4. #4
    SitePoint Addict kduv's Avatar
    Join Date
    May 2012
    Location
    Maui, HI
    Posts
    211
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    In cases like this I'm usually a fan of @StarLion's example.
    Keith
    Freelance web developer
    http://www.duvalltech.com/

  5. #5
    SitePoint Wizard bronze trophy conradical's Avatar
    Join Date
    Jan 2005
    Location
    Atlanta
    Posts
    2,354
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    @StarLion, can you give me an example. I don't know enough PHP to grasp that concept. An example would work great!

  6. #6
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,609
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    $states
    =array('AAA','BBB','CCC'); //Your array of States
    $sel='BBB'//The value retrieved from the form
    ?>
    <form>
    <select name="state">
    <?php
    foreach ($states as $state) {
        
    $opt='<option value="'.$state.'"';
        if (
    $sel==$state) { $opt .= ' selected="selected"'; }
        
    $opt .= '>'.$state.'</option>';
        echo 
    $opt;
    }
    ?>
    </select>
    </form>
    Ian Anderson
    www.siteguru.co.uk

  7. #7
    SitePoint Wizard bronze trophy conradical's Avatar
    Join Date
    Jan 2005
    Location
    Atlanta
    Posts
    2,354
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    siteguru - thanks for that. I think this is a good lesson in arrays in general. I appreciate you doing that. Ill have to give it a shot when I get home.


Bookmarks

Posting Permissions

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