I've been having a problem with my 2 dynamic drop down boxes, well they actually work but I am not sure how to store the data in mySQL database. The first drop down box is loaded with all the states and depending on what the user chooses, the second drop down box loads the cities for that state. This works..
My problem is that the code for the drop down boxes is not mine, I got it from a free script place to use. What I need to know is with my code ( i will post it below) how do I save what the user chooses for state and city? I am not sure where the data is being held at. Also, if a user comes back then I need that state and city that the user choose from before to auto load in those drop down menus. Any help will be great.
Here is my code for the drop down boxes:
<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.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="'.$stateid.'"'.'>'.$select_state.'</option>'; //please select a state none
while(list($stateid,$select_state) = mysql_fetch_row($result)){ //(list($id,$state)
echo '<option value="'.$stateid.'"'.'>'.$select_state.'</option>';
}
echo '</select>';
?>
</td>
<td>
<div id="lcontainer">
<select name="cities">
<option value="unselected">Please select a state first</option>
</select>
</div>
And here is the code for getcities.php that is referenced above:
<?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>';
?>
Thanks in advance
Scott