Hello,
I am trying to implement two cascading <select> boxes. the second box's options would be fetched from a mysql db via a PHP script.
it works fine on IE.
Problems rise when it comes to Firefox, the form would not submit the value from the second <select> box.
I have the feeling that we need to return the name/value pairs of data and generate the <option>s on client-side, I just dont know how to do it using DOM.
Please advise..
Below is code for both the JS and PHP scripts:
PHP script
<?php
$countrycode=$_GET["countrycode"];
include("key.php");
$connection = mysql_connect($mysql_host_name,$username, $password) or die(mysql_error());
$db = mysql_select_db($mysql_db_name,$connection) or die ("Couldn't select database.");
mysql_select_db("getCities", $connection);
$sql="SELECT city_id, countrycode, name, nr_hotels FROM getCities WHERE countrycode = '".$countrycode."' ORDER BY name";
$result = mysql_query($sql);
echo "
<select name=\"city_id\" style=\"font-family:Verdana, Arial, Helvetica, sans-serif; font-weight: bold; font-size:15px \">
<OPTION VALUE=\"0\">-----------------------------------------
";
$options="";
while($row = mysql_fetch_array($result))
{
$cityname=$row["name"];
$city_id=$row["city_id"];
$nr_hotels=$row["nr_hotels"];
$options.="<OPTION VALUE=\"$city_id\">".$cityname." (".$nr_hotels." hotels)";
}
echo $options;
echo "</select>";
mysql_close($connection);
?>
Javascript
var xmlHttp
function showUser(str)
{
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}
var url="ajax.php"
url=url+"?q="+str
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}
function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById("txtHint").innerHTML=xmlHttp.responseText
}
}
function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
//Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}