Is it possible to hide a value in a form drop down list (PHP, JavaScript)?
I have a form drop down box (for the 50 states) that is populated via a PHP for loop (see code below). The user selects a residential state, then later in the form also has to select a mailing state (in case the two are different). I have provided a check box in JavaScript that, when checked, populates all the mailing information with the information that has already been entered in the residential information section. When that box is checked all the mailing information fields except the mailing state are disabled and read same as residential information
. The mailing state does not read same as residential information
because I did not populate it with that option. Is there some way I can populate my drop down box wit开发者_运维百科h that option so the user cannot see it, but it will still be available when I want to use it in the code?
<label>Residental State</label><br/>
<select id="res_state" name="res_state">
<?php
$states = array('Select State' , 'AA' , 'AE' , 'AK' , 'AL' , 'AP' , 'AR' , 'AS' , 'AZ' , 'CA' , 'CO' , 'CT', 'DE' , 'DC' , 'FL' , 'FM' , 'GA' , 'GU' , 'HI' , 'IA' , 'ID' , 'IL' , 'IN' , 'KS' , 'KY' , 'LA' , 'MA' , 'MD' , 'ME' , 'MH' , 'MI' , 'MN' , 'MO' , 'MP' , 'MS' , 'MT' , 'NC' , 'ND' , 'NE' , 'NH' , 'NJ' , 'NM' , 'NV' , 'NY' , 'OH' , 'OK' , 'OR' , 'PA' , 'PR' , 'PW' , 'RI' , 'SC' , 'SD' , 'TN' , 'TX' , 'UT' , 'VA' , 'VI' , 'VT' , 'WA' , 'WI' , 'WV' , 'WY');
for($i = 0; $i < count($states); $i++){
echo "<option>$states[$i]</option>";
}
?>
</select>
Disable all the form controls and populate them with the actual values from the first form.
This is the typical behavior users will expect. If you write same as residential information
everywhere your site will look unprofessional from a user experience point of view.
Rather than hiding the option and then showing it, you could just add it with your JavaScript:
theSel = document.getElementById('res_state');
theSel.options[theSel.length] = new Option('same as residential information', 'value');
精彩评论