Hi
Im trying to make three dependant drop down boxes which get information from a database with Ajax and PHP. Everything works when I select a number in the first drop down box but if I select a word then the second drop down box works but the third one does not <---how strange is that lol. Ive been looking at this for three whole days lol. Can anyone help me??
Second drop down box is made from here (findcategory.php)...PHP Code:
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="javascript" type="text/javascript">
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 getone(category) {
var strURL="findcategory.php?category="+category;
var req = getXMLHTTP();
if (req) {
req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
document.getElementById('fnamediv').innerHTML=req.responseText;
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
function gettwo(category,fname) {
var strURL="findcomments.php?category="+category+"&fname="+fname;
var req = getXMLHTTP();
if (req) {
req.onreadystatechange = function() {
if (req.readyState == 4) {
document.getElementById('commentsdiv').innerHTML=req.responseText;
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
</script>
</head>
<body>
<?php
$link = mysql_connect('localhost', '', ''); //changet the configuration in required
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('');
$query="SELECT Distinct category FROM test";
$result=mysql_query($query);
?>
<form method="post" action="" name="form1">
<table width="60%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="150">category</td>
<td width="150"><select name="category" onChange="getone(this.value)">
<option value="">Select category</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value="<? echo $row["category"]?>"><? echo $row["category"]?></option>
<? } ?>
</select></td>
</tr>
<tr style="">
<td>name</td>
<td ><div id="fnamediv"><select name="fname" >
<option>Select category First</option>
</select></div></td>
</tr>
<tr style="">
<td>comments</td>
<td ><div id="commentsdiv"><select name="comments" >
<option>Select name First</option>
</select></div></td>
</tr>
</table>
</form>
</body>
</html>
the third drop box gets its results from here (findcomments.php)....PHP Code:
<?php
$category= $_GET["category"];
echo $category;
$link = mysql_connect('localhost', '', '');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('');
$query="SELECT * FROM test WHERE category='$category'";
$result=mysql_query($query);
?>
<select name="fname" onchange="gettwo(<? echo $category; ?>,this.value)">
<option>Select name</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value="<? echo $row["fname"]?>"><? echo $row["fname"]?></option>
<? } ?>
</select>
PHP Code:
<?
$category= $_GET["category"];
$fname= $_GET["fname"];
echo $category;
echo "<br>";
echo $fname;
$link = mysql_connect('localhost', '', ''); //changet the configuration in required
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('');
$query="SELECT id,comments FROM test WHERE fname='$fname' AND category='$category'";
$result=mysql_query($query);
?>
<select name="comments">
<option>Select comments</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value><?=$row["comments"]?></option>
<? } ?>
</select>


Reply With Quote
. Someone mentioned the problem could be because their are blank spaces somewhere when the PHP code is used in conjunction with Javascript.


Bookmarks