PDA

View Full Version : Changing state field if there is choosen country USA?



toplisek
10-30-2006, 01:07 PM
I have seen in web sites that on registration page there is no state but if there is selected country USA it will refresh page and show field with list boy states. Do you know how to do it with function? Need help:)

djr33
10-30-2006, 11:39 PM
This isn't PHP.

It's javascript.

Something like:

(This code is likely/probably wrong, but it gives you the idea... it may also need to be customized more to your page.)

<select onChange="if (this.value == 'USA') visibility.state = 'visible';">
....options....
<option value="USA">USA</option>
</select>
<input type="text" id="state" style="visibility:hidden">

something like that.

Basically, when the select menu changes, the "visibility:hidden" of the state input (use a select if you want... whatever) is set to visible, so it's then shown.

(You should also add else visibility='hidden', so it goes away if it was changed)


again, just outline o fthe code, look up the syntax...

thetestingsite
10-31-2006, 12:40 AM
Although what djr33 stated above is true, this could be pulled off using PHP; however javascript would be the best method of doing this. Being that you would not need to send data to the server, and have the user wait while the page reloads and whatnot, you could simply do this all on the client side.

djr33
10-31-2006, 02:55 AM
No. PHP could NOT do this.

Yes, it could check that, but it would require, as you said, the page reloading, which would defeat the point, really.

The only time PHP could be used for this is in a multi-page form, where, for example, each page had one field and it kept going, like a test, or survey or something.

But javascript is the way to make this happen live.

Also, I think the better way to do it is this--
1. use <body onLoad="..."> to set the field(s) to visibility of hidden (I'm not sure if that's fieldname.visibility or fieldname.style, or something else... look it up).
2. use onChange in the select menu(s)/etc. to check if it's NOT equal to "USA"/etc. and set it to hidden. Otherwise, set it to visible.
3. Don't, in the html code, use style="visibility:hidden".

Reason:
If javascript isn't enabled, it would show up. Otherwise, the user would never be able to access the field.