开发者

Issue with 'selected' value within form

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!


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜