Hi there guys,

I'm still working on my form, and part of the form is an age select dropdown. I'm leading them through an approval process and if they'd like, returning them to the form to fix any errors.

The only way I know of having it show up correct in the dropdowns is to do an if statement on each option from the $_POST data. This is cumbersome, as I have 3 selection boxes with many options in each.

Is there a better way to do this than to create an if statement for each option?

thanks,
json

    Is this the sort of thing you're talking about?

    <select name='age'>
    <?php
    foreach(range(18,99), as $age)
    {
       $selected = (isset($_POST['age'] and $_POST['age'] == $age) ? " selected='selected'" : '';
       echo "<option$selected>$age</option>\n";
    }
    ?>
    </select>
    

      man, that is snazzy. Thanks very much for that NogDog. The time saved is immeasurable.

      thanks,
      json

        Hi there NogDog,

        Seems I may have muddled something up. Here is the way I implemented it:

        			<select name='dob_y'>");
        
        		foreach(range(1940,2000), as $dob_y){
        			$selected = (isset($_SESSION['dob_y'] and $_SESSION['dob_y'] == $dob_y) ? " selected='selected'" : '';
        			echo "<option$selected>$dob_y</option>\n";
        		}
        		echo("</select>");
        

        This is the error I get:

        Parse error: syntax error, unexpected ','

        The line it's in reference to:

        foreach(range(1940,2000), as $dob_y){
        

        Any help would be greatly appreciated.

        thanks,
        json

          There should not be a comma after the closing parenthesis of the range() call.

            Hi there NogDog,

            That took care of the line, thanks. I now have an error one line lower:

            $selected = (isset($_SESSION['dob_m'] and $_SESSION['dob_m'] == $dob_m) ? " selected='selected'" : '';
            

            Error:

            Parse error: syntax error, unexpected T_LOGICAL_AND, expecting ',' or ')' in /var/www/html/briar/application.php on line 514

            I'm pretty sure it's a missing ending closing parenthisis, but I don't know where it should go.

            thanks,
            json

              isset($_SESSION['dob_m'][color=red][b])[/b][/color]
              

                Hi there again,

                That fixed me up spectacularly. I do have another question however(sorry):

                Is there a way to do this with a state listing type dropdown?

                <select name='state' id='state'>
                	<option value='AL'>Alabama</option>
                	<option value='AK'>Alaska</option>
                	<option value='AZ'>Arizona</option>
                	<option value='AR'>Arkansas</option>
                	<option value='CA'>California</option>
                	<option value='CO'>Colorado</option>
                	<option value='CT'>Connecticut</option>
                	<option value='DE'>Delaware</option>
                	<option value='DC'>District of Columbia</option>
                	<option value='FL'>Florida</option>
                	<option value='GA'>Georgia</option>
                	<option value='HI'>Hawaii</option>
                	<option value='ID'>Idaho</option>
                	<option value='IL'>Illinois</option>
                	<option value='IN'>Indiana</option>
                	<option value='IA'>Iowa</option>
                	<option value='KS'>Kansas</option>
                	<option value='KY'>Kentucky</option>
                	<option value='LA'>Louisiana</option>
                	<option value='ME'>Maine</option>
                	<option value='MD'>Maryland</option>
                	<option value='MA'>Massachusetts</option>
                	<option value='MI'>Michigan</option>
                	<option value='MN'>Minnesota</option>
                	<option value='MS'>Mississippi</option>
                	<option value='MO'>Missouri</option>
                	<option value='MT'>Montana</option>
                	<option value='NE'>Nebraska</option>
                	<option value='NV'>Nevada</option>
                	<option value='NH'>New Hampshire</option>
                	<option value='NJ'>New Jersey</option>
                	<option value='NM'>New Mexico</option>
                	<option value='NY'>New York</option>
                	<option value='NC'>North Carolina</option>
                	<option value='ND'>North Dakota</option>
                	<option value='OH' selected='yes'>Ohio</option>
                	<option value='OK'>Oklahoma</option>
                	<option value='OR'>Oregon</option>
                	<option value='PA'>Pennsylvania</option>
                	<option value='RI'>Rhode Island</option>
                	<option value='SC'>South Carolina</option>
                	<option value='SD'>South Dakota</option>
                	<option value='TN'>Tennessee</option>
                	<option value='TX'>Texas</option>
                	<option value='UT'>Utah</option>
                	<option value='VT'>Vermont</option>
                	<option value='VA'>Virginia</option>
                	<option value='WA'>Washington</option>
                	<option value='WV'>West Virginia</option>
                	<option value='WI'>Wisconsin</option>
                	<option value='WY'>Wyoming</option>
                </select>
                

                Or, am I better served doing an if statement for each option?

                thanks,
                json

                  You can do the same thing using an array, but you'll need to manually type out the array elements:

                  $states = array(
                     'AL' => 'Alabama',
                     'AK' => 'Alaska',
                  // etc...
                     'WY' => 'Wyoming'
                  );
                  foreach($states as $abbrev => $name)
                  {
                     $selected = ($abbrev == $_POST['state']) ? " selected='selected'" : '';
                     echo "<option value='$abbrev'$selected>$name</option>\n";
                  }
                  
                    Write a Reply...