I am creating a website that allows a user to fill out some information, some of this includes selecting a state and city from a drop down box. I am able to get this to work for the user to use but I am not sure as to how I go about saving what they select in my mySQL database. Also, if the user comes back to this page I want all their data to pre-load for them. I am unsure as to how to pre-load these drop-down boxes with what was saved in the database.
Here is my code:
[CODE]
<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 greatly appreciated.
Thanks,
Scott