Hi,
I have this script…that linked two drop-down box, the original script is that this is in two separate page that i combined…
i works fine but when i select in drop-down box (Category) it echo out again the Category drop-down box.
Can anyone help me with this.
Thanks a lot…
<?php
include_once("conn.php");
?>
<html>
<head>
<script type="text/javascript">
function showUser(str)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","main.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<table>
<?php
$cat=mssql_query("Select * from tblCat");
echo '<select name="users" onchange="showUser(this.value)">';
echo '<option value="">Select a person:</option>';
while($row=mssql_fetch_assoc($cat)){
echo "<option value='".$row['Catid']."'>".$row['CatName']."</option>";
}
echo '</select>';
?>
</table>
<br />
<div id="txtHint"><b>
<?php
$q=$_GET["q"];
$result = mssql_query("SELECT * FROM tblSubCat WHERE CatID = '".$q."'");
echo "<table >";
echo "<tr>";
echo '<td align="right"><b>Sub-Category <font color=red>*</font>: </b></td>';
echo "<td>";
echo "<select class='subcatid' name='subcatid' tabindex='2'>";
while($subcat=mssql_fetch_array($result)){
echo "<option value='" . $subcat['SubCatName'] . "'>" . $subcat['SubCatName'] . "</option>";
}echo '</select>';
echo "<td>";
echo "</tr>";
echo "</table>";
?>
</b></div>
</body>
</html>
You are using AJAX to call main.php when the user selects a value in the first drop down.
What is the code of main.php? It should contain only that part of the code you posted that is needed to get the result you want to show in txtHint.
So if you’re calling the entire script you posted here, then you’ll have to create a second script that contains only this part and call that in your XMLHttpRequest:
<?php
include_once("conn.php");
$q= $_GET["q"];
$result = mssql_query("SELECT * FROM tblSubCat WHERE CatID = '".$q."'");
echo "<table >";
echo "<tr>";
echo '<td align="right"><b>Sub-Category <font color=red>*</font>: </b></td>';
echo "<td>";
echo "<select class='subcatid' name='subcatid' tabindex='2'>";
while($subcat=mssql_fetch_array($result)){
echo "<option value='" . $subcat['SubCatName'] . "'>" . $subcat['SubCatName'] . "</option>";
}echo '</select>';
echo "<td>";
echo "</tr>";
echo "</table>";
?>
By the way, sanitize the user input ($_GET[‘q’]) before using it in a query to avoid sql injection.
Thanks for the reply…
I’am newbie in ajax.so i’m lost can you help me.
Thanks.
this is the code for the main.php
<?php
include_once("conn.php");
$cat=mssql_query("Select * from tblCat");
?>
<html>
<head>
<script type="text/javascript">
function showUser(str)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getsubcat.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<?php
echo $_POST['subcatid'];
echo $_POST['namecat'];
?>
<form>
<table>
<?php
echo '<select name="users" onchange="showUser(this.value)">';
echo '<option value="">Select a person:</option>';
while($row=mssql_fetch_assoc($cat)){
echo "<option value='".$row['Catid']."'>".$row['CatName']."</option>";
}
echo '</select>';
?>
</table>
</form>
<br />
<div id="txtHint"><b>Person info will be listed here.</b></div>
</body>
</html>
And for the getsubcat.php
<?php
include_once("conn.php");
$q=$_GET["q"];
$result = mssql_query("SELECT * FROM tblSubCat WHERE CatID = '".$q."'");
echo "<table >";
echo "<tr>";
echo '<td align="right"><b>Sub-Category <font color=red>*</font>: </b></td>';
echo "<td>";
echo "<select class='subcatid' name='subcatid' tabindex='2'>";
while($subcat=mssql_fetch_array($result)){
echo "<option value='".$subcat['SubCatName']."'>".$subcat['SubCatName']."</option>";
}echo '</select>';
echo "<td>";
echo "</tr>";
echo "</table>";
?>
Yes, that’s what I meant. Does it work?
If not, what happens? An error?