scarlson Posted December 9, 2007 Share Posted December 9, 2007 Ok, I have 2 drop down boxes that pre-load the states and then corresponding cities in another drop down box for users to select. My question is, how do I save what they select to mySQL database I have? Also, once they select something, how do I go about pre-loading this data in the drop down boxes once they return to the page again to edit? Here is the code I have : <td><script type="text/javascript"> function createRequestObject() { var req; if(window.XMLHttpRequest){ // Firefox, Safari, Opera... req = new XMLHttpRequest(); } else if(window.ActiveXObject) { // Internet Explorer 5+ req = new ActiveXObject("Microsoft.XMLHTTP"); } else { // There is an error creating the object, // just as an old browser is being used. alert('Problem creating the XMLHttpRequest object'); } return req; } // Make the XMLHttpRequest object var http = createRequestObject(); function sendRequest(id) { // Open PHP script for requests http.open('get', 'getcities.php?id='+id); http.onreadystatechange = handleResponse; http.send(null); } function handleResponse() { if(http.readyState == 4 && http.status == 200){ // Text returned FROM the PHP script var response = http.responseText; if(response) { // UPDATE ajaxTest content document.getElementById("lcontainer").innerHTML = response; } } } </script> <?php //connect to database $sql = "SELECT `id`,`state` FROM `states`";//select all our states $result = mysql_query($sql) or die(mysql_error()); echo '<select name="state" onChange="sendRequest(this.value)">';//the select box with the javascript function to change the cities echo '<option value="none">Please select a state</option>'; //please select a state none while(list($id,$state) = mysql_fetch_row($result)){ echo '<option value="'.$id.'"'.'>'.$state.'</option>'; } Any ideas would be great. Thanks in advance, Scott Link to comment https://forums.phpfreaks.com/topic/80838-drop-down-boxes/ Share on other sites More sharing options...
revraz Posted December 9, 2007 Share Posted December 9, 2007 Use $_POST['state'] (the name you picked) to get it from the form to do with what you like, assuming you are using POST method in your form. I posted code on how to populate a option box from a DB, I'll see if I can find the post. Link to comment https://forums.phpfreaks.com/topic/80838-drop-down-boxes/#findComment-410089 Share on other sites More sharing options...
scarlson Posted December 9, 2007 Author Share Posted December 9, 2007 I guess this could bring up a different question. Is having a populated state and city drop down boxes for users to choose from the best thing to do? I thought at first that this is a good idea but not so sure. I know not all the cities will be in the list for one. I just not sure what to do with bad spellings or just wrong data in general if I allow users to input this info on their own. On my main page there will be an area for others to do a search by city and state to find items in that area. Any other ideas of what I can do? Link to comment https://forums.phpfreaks.com/topic/80838-drop-down-boxes/#findComment-410096 Share on other sites More sharing options...
tommyboy123x Posted December 9, 2007 Share Posted December 9, 2007 i have a code for a dynamic (updates from a database) triple menu utilizing javascript if you'd like it..? Link to comment https://forums.phpfreaks.com/topic/80838-drop-down-boxes/#findComment-410104 Share on other sites More sharing options...
revraz Posted December 9, 2007 Share Posted December 9, 2007 Depends on what you want to do with the data. If the data needs to be the same because something depends on the spelling, then use a drop down. If you don't care if they don't know how to spell a city or state, then let them enter it. I guess this could bring up a different question. Is having a populated state and city drop down boxes for users to choose from the best thing to do? I thought at first that this is a good idea but not so sure. I know not all the cities will be in the list for one. I just not sure what to do with bad spellings or just wrong data in general if I allow users to input this info on their own. On my main page there will be an area for others to do a search by city and state to find items in that area. Any other ideas of what I can do? Link to comment https://forums.phpfreaks.com/topic/80838-drop-down-boxes/#findComment-410106 Share on other sites More sharing options...
scarlson Posted December 9, 2007 Author Share Posted December 9, 2007 i have a code for a dynamic (updates from a database) triple menu utilizing javascript if you'd like it..? That would be great. It almost sounds like the one I am using right now though. My drop down boxes work fine, I just don't know for sure how to save what a user choose and then how to take what the user choose and then update the drop down box with the selection the user had if he/she goes back to edit. Link to comment https://forums.phpfreaks.com/topic/80838-drop-down-boxes/#findComment-410115 Share on other sites More sharing options...
revraz Posted December 9, 2007 Share Posted December 9, 2007 I posted above how to save what they choose. Link to comment https://forums.phpfreaks.com/topic/80838-drop-down-boxes/#findComment-410131 Share on other sites More sharing options...
revraz Posted December 9, 2007 Share Posted December 9, 2007 Here is the post where I posted the code on how to pull them out of the DB and into your form. Link to comment https://forums.phpfreaks.com/topic/80838-drop-down-boxes/#findComment-410146 Share on other sites More sharing options...
scarlson Posted December 9, 2007 Author Share Posted December 9, 2007 I don't see the link to the post. Link to comment https://forums.phpfreaks.com/topic/80838-drop-down-boxes/#findComment-410149 Share on other sites More sharing options...
revraz Posted December 9, 2007 Share Posted December 9, 2007 http://www.phpfreaks.com/forums/index.php/topic,171058.msg756783.html#msg756783 Link to comment https://forums.phpfreaks.com/topic/80838-drop-down-boxes/#findComment-410181 Share on other sites More sharing options...
scarlson Posted December 9, 2007 Author Share Posted December 9, 2007 Can someone explain to me what this code is doing? <?php //connect to database $sql = "SELECT `id`,`state` FROM `states`";//select all our states $result = mysql_query($sql) or die(mysql_error()); echo '<select name="state" onChange="sendRequest(this.value)">';//the select box with the javascript function to change the cities echo '<option value="none">Please select a state</option>'; //please select a state none while(list($id,$state) = mysql_fetch_row($result)){ echo '<option value="'.$id.'"'.'>'.$state.'</option>'; } echo '</select>'; ?> I don't understand where the $id and $state is coming from, it's not initialized any where. This all has to do with my drop down boxes for States and Cities. I am still having a very hard time understanding how to work this. I have a $stateid (which is equal to the id stored in the database for that user - manually placed this in the database for now) and a $select_state (which is equal to the name of the State stored in the database for that user - manually placed this in the database as well). http.open('get', 'getcities.php?id='+id); I have this bit of code that is used to select the cities. I am not sure what the 'getcities.php?id ='+id) is doing. I see that it's using the getcities.php file but not sure what the ?id='+id is for. <?php include "dbconnect.php"; //connect to database $id = $_GET['id']; $sql = "SELECT `id`,`city` FROM `cities` WHERE `state_id`=$id";//select the cities from the given state $result = mysql_query($sql) or die(mysql_error()); //display the contents of our div tag echo '<select name="cities">'; while(list($id,$city) = mysql_fetch_row($result)){ echo '<option value="'.$id.'">'.$city.'</option>'; } echo '</select>'; ?> This is the getcities.php Link to comment https://forums.phpfreaks.com/topic/80838-drop-down-boxes/#findComment-410336 Share on other sites More sharing options...
revraz Posted December 9, 2007 Share Posted December 9, 2007 They probably have register globals on, so they get created automatically. You need to assign the variables to make it work for your purpose. The city is probably the ID of the city in a DB. Link to comment https://forums.phpfreaks.com/topic/80838-drop-down-boxes/#findComment-410344 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.