views:

47

answers:

3

Hi all, 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 with 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>
A: 

you can add style="display:none;" to that option:

echo "<option style="display:none;">$states[$i]</option>";
kgb
This doesn't work. At least, not in Chrome, Internet Explorer and Opera. In opera it hides the text but the option is still selectable.
Andy E
+1 for a better option, but please don't put display code (HTML) inside functional code...
Unkwntech
doesn't work in Fire Fox or IE either.
typoknig
This does work in Firefox 3.6 for sure. Not sure about earlier versions.
John Conde
When I moved this outside the PHP code and just did this last option in HTML it works in Fire Fox 3.6, but it still does not work in IE8.
typoknig
You need to escape the quotes around display:none;, or else use single quotes.
john
+4  A: 

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.

Ben S
I had originally done what you are suggesting, but if the user checks the `same as` box and then goes back and changes the `residential information` the `mailing information` does not update. I was trying to avoid writing another function that constantly checked the value of the `residential information` fields.
typoknig
Users know about this and won't check the box until they're done. If you're still worried about it. Just add `onchange="your_copy_method()"` to each element in the form. That way, any time something changes on the form, the other is updated.
Ben S
Haha, I don't know why I didn't think of that! I was thinking I was going to have to make some nasty function to check the `residential fields` every few seconds or something. I did what you suggested and it works great! Thanks a lot!
typoknig
A: 

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');
john