Jump to content

Auto drop down box help needed please


CoffeeAddict

Recommended Posts

I hope this is the right forum for this question. I need to create an auto drop down box. For example I already have a list of states that are pulled from the database. When the user selects a state, a drop down box below should populate with the cities from that state. (that's where I'm having trouble)

 

This is a search form so the results have to reflect what city and state they chose. (along with some other criteria)

 

I've searched all evening for instructions on how to do this and so far I've only seen javascript that you put directly in the code. If I have all 50 states along with their related cities that's an awful lot of code to put in the page.

 

Isn't there a way to automatically pull the cities from the database to populate the box when the user clicks a state? I have no idea where to start to make that work.

Link to comment
https://forums.phpfreaks.com/topic/41552-auto-drop-down-box-help-needed-please/
Share on other sites

the only way I know of to react to a users choices would be javascript. php will not do it because it is a server side language meaning it only gets executed on the server. Javasript is a client side language and allows you to set event handlers to react when a user performs a specific action.

 

I don't know javascript to save my life so I can not be of more help, but this is where I would begin.

 

You may be able to find some ajax routines that will allow you to use php to do it, but again javascript is what your going to have to use to make the server calls when the user clicks a particular state.

ok...

 

 

firstly if the results are in the database, you either need to use AJAX ( javascrit extension (can it use databases???)) or you need to use php to get these values... if you dont want the page to refresh when you click a state, you need javascript AJAX, but id use php.  if there arnet to meny fields to retreive, then get the with php, ie $SQL = 'SELECT * from States'; then send it $QUERY = mysql_query($SQL, dbconnection);  and the walk through the array results.... $Results = mysql_fetch_array($QUERY), foreach($Results) {STUFF HERE}

 

thats most of the php done.  al you need to do is write the values to the javasvript code.. 

 

 

now im not to great with javascvript, but im doing something similar as we speak.  roughly the code would be an attribute in the html tag <menu onClick = "javascriptFunctiontoDoStuffhere">  that function would populate the next field.  something like, document.window.formname.menu.optioname.value() = '$New_Value'

 

 

anyway, good luck, hope that helps...

 

c ya

This is so confusing.

 

I don't want the page to refresh, so then I need to use AJAX?

 

I can probably find a javascript somewhere that will populate the second drop down box, but then I can't figure out how to make it return the correct search results. Am I making any sense? I don't just want the box to populate, I want it to work when the user hits that search button.

Yes, you will need ajax.

 

ajax will use javascript to send a server request at which point you will run a sql query and it will return the results all without the page refreshing.

 

 

If you have values set correctly in your dropdown, then if you can get the proper pieces to populate, then on submit it SHOULD use the values set in the form and search for that item.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.