Hi all,
I mistakingly posted this in the PHP forum but hope to find my solution here. I am trying to make a dropdown system for states and cities but the cities aren't coming up correctly.
I have this on the root page:
<?
echo "<form name=sel>\n";
echo "States : <font id=states><select>\n";
echo "<option value='0'>============</option> \n" ;
echo "</select></font>\n";
echo "Cities : <font id=cities><select>\n";
echo "<option value='0'>=== none ===</option> \n" ;
echo "</select></font>\n";
?>
<script language=Javascript>
function Inint_AJAX() {
try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {} //IE
try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} //IE
try { return new XMLHttpRequest(); } catch(e) {} //Native Javascript
alert("XMLHttpRequest not supported");
return null;
};
function dochange(src, val) {
var req = Inint_AJAX();
req.onreadystatechange = function () {
if (req.readyState==4) {
if (req.status==200) {
document.getElementById(src).innerHTML=req.responseText; //retuen value
}
}
};
req.open("GET", "state.php?data="+src+"&val="+val); //make connection
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=iso-8859-1"); // set Header
req.send(null); //send value
}
window.onLoad=dochange('states', -1); // value in first dropdown
</script>
And calling on this script:
<?
//set database
define("_VALID_PHP", true);
require_once("../include/config.php");
if (isset($_POST['login']))
: $result = $session->login($_POST['username'], $_POST['password'], isset($_POST['remember']));
// Login successful
if ($result && $session->userlevel == '2')
: redirect("useredit.php");
elseif ($result && $session->userlevel == '1')
: redirect("agentedit.php");
elseif ($result && $session->userlevel == '9')
: redirect("admin/index.php");
else : redirect("index.php");
endif;
endif;
$data=$_GET['data'];
$val=$_GET['val'];
switch($data){
case "states": // $data == "states"
echo "<select name='state' onChange=\"dochange('cities', this.value)\">\n";
echo "<option value='0'>==== Choose State ====</option>\n";
$sql = "SELECT id, state FROM users ORDER BY state";
$result = $db->query($sql);
while ($row = $db->fetch($result)) {
$state = $row['state'];
echo "<option value=\"". $state . "\">" . $state . "</option> \n";
}
break;
case "cities": // $data == "cities"
echo "<select name='cities'>\n";
echo "<option value='0'>==== Choose City ====</option>\n";
$sql = "SELECT id, city FROM users WHERE id = '$val' ORDER BY city ";
$result = $db->query($sql);
while ($row = $db->fetch($result)) {
$city = $row['city'];
echo "<option value=\"". $city . "\">" . $city . "</option> \n";
}
break;
}
echo "</select>\n";
?>
Really appreciate any help on this and look forward to your answer.
Cheers...