views:

7

answers:

1

I currently have a form built in which after validation, if errors exist, the data stays on screen for the consumer to correct. An example of how this works for say the 'Year of Birth' is:

<select name="DOB3">
    <option value="">Year</option>
    <?php
        for ($i=date('Y'); $i>=1900; $i--)
  {
    echo "<option value='$i'";
    if ($fields["DOB3"] == $i)
      echo " selected";
    echo ">$i</option>";
  }
  ?>
</select>

If an error is found, the year of birth value returns the year previously entered. I am able to have this work on all field with the exception of my 'State' field. I build the array and function for the drop down with the following code:

<?php

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

    function showOptionsDrop($array, $active, $echo=true){
        $string = '';

        foreach($array as $k => $v){
            $s = ($active == $k)? ' selected="selected"' : '';
            $string .= '<option value="'.$k.'"'.$s.'>'.$v.'</option>'."\n";     
        }

        if($echo) {   echo $string;}
        else {       return $string;}
    }
?> 

I then call the function from within the form using:

<td><select name="State"><option value="">Choose a State</option><?php showOptionsDrop($states_arr, null, true); ?></select></td>

Not sure what I'm missing but would love any assistance if somebody sees the error in my code.

Thanks!

+1  A: 

Have a look at the code:

<?php showOptionsDrop($states_arr, null, true); ?>

You are passing null, so $active will always be null. The condition

($active == $k)

will never we evaluate to true.

You should pass the value you get from the form instead, e.g.:

<?php showOptionsDrop($states_arr, isset($fields['State']) ? $fields['State'] : null,  true); ?>

You really should try to separate the PHP from the HTML, especially in your first example

Update:

Actually it is not that much, but consider to use the alternative syntax for control structures:

<select name="DOB3">
    <optgroup label="Year">
    <?php for ($i=date('Y'); $i>=1900; $i--) : ?>
        <option value="<?php echo $i ?>" 
             <?php echo ($fields["DOB3"] == $i) ? 'selected="selected"' : '' ?> > 
             <?php echo $i ?> 
        </option>
    <?php endforeach; ?>
    </optgroup>
</select>

Also if you want to give the options some kind of label, you can do this with the optgroup HTML tag (this is not selectable).

Felix Kling
@Felix - thank you for the spot. I have been staring at that line for an hour now! What specifically do you mean by separating the PHP and HTML, especially in that particular instance. I need to create a form with years selection criteria and doing so in the first instance leaves a lot of code.
JM4
@JM4: See my updated answer. Actually is not too bad (I thought that it might be better to generate the values as an arras beforehand but it does not make a difference). But imo, the alternative syntax es way more readable and it is easier to spot errors in your HTML. But you could consider to set the current year and the year up to one can select beforehand.
Felix Kling