OK, I have downloaded a couple of scripts that should, when working correctly, populate a second dropdown based on what the first choice is. These work using the OLD mysql stuff but now I am trying to update it for PDO. The PDO connect routine works since I use it on other projects.
So, here is the INDEX.php file that populates the first dropdown, the javascript ajax routine should call findcity.php and populate the city dropdown based on the country (first) dropdown.
Roshan's Ajax dropdown code<?
#### Roshan's Ajax dropdown code with php
#### Copyright reserved to Roshan Bhattarai - nepaliboy007@yahoo.com
#### if you have any problem contact me at http://roshanbh.com.np
#### fell free to visit my blog http://php-ajax-guru.blogspot.com
?>
<script>
function getXMLHTTP() { //fuction to return the xml http object
var xmlhttp=false;
try{
xmlhttp=new XMLHttpRequest();
}
catch(e) {
try{
xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){
try{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e1){
xmlhttp=false;
}
}
}
return xmlhttp;
}
function getCity(strURL) {
var req = getXMLHTTP();
if (req) {
req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
document.getElementById('citydiv').innerHTML=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
</script>
</head>
<body>
<form method="post" action="" name="form1">
<table width="60%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="150">Country</td>
<td width="150"><select name="country" onChange="getCity('findcity.php?country='+this.value)">
<option value="">Select Country</option>
<option value="1">USA</option>
<option value="2">Canada</option>
</select></td>
</tr>
<tr style="">
<td>City</td>
<td ><div id="citydiv"><select name="city">
<option>Select City</option>
</select></div></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
</form>
</body>
</html>
and now for the findcity.php code:
<?
#### Roshan's Ajax dropdown code with php
#### Copyright reserved to Roshan Bhattarai - nepaliboy007@yahoo.com
#### if you have any problem contact me at http://roshanbh.com.np
#### fell free to visit my blog http://php-ajax-guru.blogspot.com
?>
<? $country=$_REQUEST['country'];
include_once(getRootPath(). 'inc/db_connect_PDO2.php');
$db = get_db_connection();
$result = $db->prepare('SELECT * FROM city WHERE contryid = :country');
$result = $db->bindParam(':country',$country);
$result->execute();
// $link = mysql_connect('localhost', 'sacccco1_dbuser', 'dbuser'); //changet the configuration in required
// if (!$link) {
// die('Could not connect: ' . mysql_error());
// }
// mysql_select_db('sacccco1_MainDB');
// $query="select city from city where countryid=$country";
// $result=mysql_query($query);
print_r($result->fetchALL());
?>
<select name="city">
<option>Select City</option>
// <? while($row=mysql_fetch_array($result)) { ?>
<? while($row = $result->fetch(PDO::FETCH_NUM)) { ?>
<option value><?=$row['city']?></option>
<? } ?>
</select>
<?php
// returns the relative path from current folder to Web Site Root directory
function getRootPath() {
$current_path = pathinfo($_SERVER['SCRIPT_NAME'], PATHINFO_DIRNAME);
$current_host = pathinfo($_SERVER['REMOTE_ADDR'], PATHINFO_BASENAME);
$the_depth = substr_count( $current_path , '/');
// Set path to root for includes to access from anywhere
if($current_host == '127.0.0.1') $pathtoroot = str_repeat('../' , $the_depth-1);
else $pathtoroot = str_repeat ('../' , $the_depth);
return $pathtoroot;
}
?>
Please let me know what I did wrong.
E