I have two ajax mysql php 2 drop down select that I am populating... i'm doing something stupid... anyone have any suggestions... or pointers....?
1.php
<?php require_once('dbconfig.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$colname_country = "-1";
if (isset($_GET['countryid'])) {
$colname_country = $_GET['countryid'];
}
mysql_select_db($database_dbfacelist, $dbfacelist);
$query_country = sprintf("SELECT * FROM country WHERE countryid = %s", GetSQLValueString($colname_country, "text"));
$country = mysql_query($query_country, $dbfacelist) or die(mysql_error());
$row_country = mysql_fetch_assoc($country);
$totalRows_country = mysql_num_rows($country);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
function get_cities(countryid)
{
$.ajax({
type: "POST",
url: "cities.php", /* The country id will be sent to this file */
beforeSend: function () {
$("#city").html("<option>Loading ...</option>");
},
data: "countryid="+countryid,
success: function(msg){
$("#city").html(msg);
}
});
}
</script>
</head>
<body>
<?php
$sql_country = "SELECT * FROM country";
$result_country = mysql_query($sql_country);
echo "<select name='country' onChange='get_cities(this.value)'>"; //get_cities is defined below
while($row_country = mysql_fetch_array($result_country))
{
echo "<option value='".$row_country['id']."'>".$row_country['country']."</option>";
}
echo "</select>";
echo "<select name='city' id='city'></select>"; //We have given id to this dropdown
?>
</body>
</html>
<?php
mysql_free_result($country);
?>
-------------------------------------
cities.php
<?php require_once('dbconfig.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
mysql_select_db($database_dbfacelist, $dbfacelist);
$query_city = "SELECT * FROM city";
$city = mysql_query($query_city, $dbfacelist) or die(mysql_error());
$row_city = mysql_fetch_assoc($city);
$totalRows_city = mysql_num_rows($city);
// Code for cities.php
$countryid = $_REQUEST['countryid'];
$sql_city = "SELECT * FROM city WHERE countryid = '".$countryid."'";
$result_city = mysql_query($sql_city);
echo "<select name='city'>";
while($row_city = mysql_fetch_array($result_city))
{
echo "<option value='".$row_city['id']."'>".$row_city['city']."</option>";
}
echo "</select>";
mysql_free_result($city);
?>