Consider the following relational database:
Project (P_Group No,
Project_Tiltle) Student (Seat_no, Name, Class, P_Group_No) Write an AJAX script
to accept project title and display list of students those who are working in a
particular project.
Select.php
<?php
$database="test";
//database name
$q=$_GET["q"];
$con=@mysqli_connect("localhost","root"
,"", "test");//for wamp 3rd feild is balnk
if(!$con)
{
die('Could not connect:
' . @mysql_error());
}
//@mysqli_select_db("$database",$con);
$result=@mysqli_query($con,"SELECT
* FROM projects,projectstudents WHERE
projects.P_Group_No=projectstudents.P_Group_No AND projects.P_Group_No=
'$q'")or die(@mysqli_error());
echo"<table
border='1'>";
echo"<tr><th>sno</th><th>sname</th><th>Class</th></tr>";
while($row=@mysqli_fetch_array($result))
{
echo"<tr><td>";
echo $row['Seat_no'];
echo"</td><td>";
echo $row['Name'];
echo"</td><td>";
echo $row['Class'];
echo"</td></tr>";
}
echo"</table>";
@mysqli_close($con);
?>
Students.php
<html>
<head>
<script>
function showHint(str)
{
if (str=="")
{
document.getElementById("mydiv").innerHTML
= "";
return;
}
if(window.XMLHttpRequest)
{
XHRobj=new
XMLHttpRequest();
}
else
{
XHRobj=new
ActiveXObject("Microsoft.XMLHTTP");
}
XHRobj.onreadystatechange
= function()
{
if (XHRobj.readyState
== 4 && XHRobj.status == 200)
{
document.getElementById("mydiv").innerHTML
= XHRobj.responseText;
}
}
XHRobj.open("GET",
"select.php?q=" + str, true);
XHRobj.send();
}
</script>
</head>
<body>
<p><b>Project
Name:</b></p>
<form>
<?php
$database="test";
//database name
$con
=mysqli_connect("localhost","root"
,"","test");//for wamp 3rd feild is balnk
if (!$con)
{
die('Could not connect:
' . @mysqli_error());
}
//mysqli_select_db($database,
$con);
$result=mysqli_query($con,"select
* from projects")or die(@mysqli_error());
?>
<SELECT
name="pname" onchange="showHint(this.value)">
<option
value="">Select projectName</option>
<?php
while($row=mysqli_fetch_array($result))
{
?>
<option
value="<?php echo $row['P_Group_No']?>"><?php echo
$row['Project_Title']?></option>
<?php }?>
</select>
</form>
<div
id="mydiv"><b> Student info......</b></div>
</body>
</html>