无法在ajax中加载php中的数据库值

时间:2017-04-02 18:30:54

标签: javascript php ajax

我是PHP的入门级开发人员并且对PHP没有任何了解,我正在尝试在选择下拉菜单时调用数据库值,到目前为止已经在教程的帮助下完成了这个..问题是当我选择第二次下拉第一次下拉是从DB获取值,但是当我从第二个下拉菜单中选择选项时,我没有在页面上显示任何数据。从第二次下拉菜单中选择一个选项后没有任何操作。

<?php
include '../connect.php';
 $unm=htmlentities($_SESSION['username']);
?>

<html>
<head>
<title> User Panel</title></head>

<body background="../image/pencilback.jpg">
<div id="welcome"><?php
echo '<align="right">'.'<h3>'."Welcome ".$unm.'</h3>';
?></div>

<link rel="stylesheet" type="text/css" href="../css/heading.css">
<table border="0" width="50%" height="150px" align="center" >
<tr width="100%" height="200px" > <td colspan="3"> <img src="../image/banner.jpg"> </td>  </tr>
</table>

<table id="tableborder" border="0" width="83%" height="40px" align="center" bgcolor="#BDBDBD" >

<td align="center"><a href="userpanel.php"><b> Take a Test</b></a> </td> 
<td align="center"><a href="myaccount.php"><b> My Account Details</b> </td>
<td align="center"><a href="testhistory.php"><b> View Test History </b> </td> 
<td align="center"><a href="feedback.php"><b> Give Feedback</b> </td>
<td align="center"><a href="../logout.php"><b> Logout</b> </td> </td>  </tr>
</table>
<form method="POST" action="">
 <div id="viewset"><center>
  Select Desired Test </center> <br></div><br><br><br><br><br><br>
 <table border="1px" align="center" cellspacing="10px" cellpadding="10px">
 <tr> <td> <select name="tstcat" id="countrydd" onChange="change_country()">
<option> Select Test Category</option>
<?php
$res=mysqli_query($connection,"select * from sub");
while($row=mysqli_fetch_array($res))
{
    ?>
    <option value="<?php echo $row["subname"]; ?>"> <?php echo $row["subname"]; ?> </option>
<?php
    }
?>

</select>
</td>
<td>
 <div id="state" align="center">
<select id="details" onChange="display_details()">
<option>Select Test</option>
</select>

</div>
 </tr> </td></table>
<br>

<div id="display">
<table border="1px" height="100px" width="30%" align="center">
<tr style=color:maroon> <th> Test Name </th> <th> No. of Questions </th> <th> Duration </th> </tr>
<tr> <td> </td> <td> </td> <td> </td> </tr>
</table>

</div>

<center> <input type="submit" name="submit" value="Go"></center>

<?php
if(isset($_POST['submit']))
{
$tstct=$_POST['tstcat'];
$tnm=$_POST['testid'];
$_SESSION['testname']=$tnm;
$_SESSION['testcatg']=$tsc;
header('location:teststart.php');
}
?>

 </div>
 </form>



 <script type="text/javascript">

 //========== FOR DROPDOWN SELECTION

 function change_country()
 {
     var xmlhttp=new XMLHttpRequest();
     xmlhttp.open("GET","ajax.php?category="+document.getElementById("countrydd").value,false);
     xmlhttp.send(null);
     document.getElementById("state").innerHTML=xmlhttp.responseText;

 }

  //========== TO LOAD TEST DETAILS

 function display_details()
 {
     var xmlhttp=new XMLHttpRequest();
     xmlhttp.open("GET","ajax2.php?testname="+document.getElementById("details").value,false);
     xmlhttp.send(null);
     document.getElementById("display").innerHTML=xmlhttp.responseText;

 }

 </script>

  </body>
 </html>

AJAX.PHP

<?php
include '../connect.php';
$category=$_GET["category"];

if($category!="")
{
$res=mysqli_query($connection,"select * from test_detail where     test_category='$category'");
echo "<select name='testid' onchange='change_test()'>";
while($row=mysqli_fetch_array($res))
{
    ?>
    <option value="<?php echo $row["test_name"];?>"> <?php echo $row["test_name"];?>  </option>
    <?php
}
echo "</select>";
}
?>

AJAX 2.PHP

<?php
include '../connect.php';
$tstname=$_GET["testname"];

if($tstname!="")
{
$res1=mysqli_query($connection,"select * from test_detail where  test_name='$tstname'");
echo "<table>";
while($row1=mysqli_fetch_array($res1))
{
    ?>

    <tr> <td> <?php echo $row1["test_name"];?>  </td> 
    <td> <?php echo $row1["total_ques"];?> </td> 
    <td> <?php echo $row1["test_dur"];?> </td> </tr>
    <?php
}
echo "</table>";
}
    ?>

1 个答案:

答案 0 :(得分:0)

我建议你检查三件事。

  1. 在id为#state的div内部的select中,你有一个onChange调用javascript函数'display_details()',你用它来加载带有详细数据的table元素。但是当你从AJAX.PHP返回select时,onchange事件有另一个函数'change_test()',它应该是'display_details()'

  2. 在文档中动态插入新的html元素时,通常必须使用动态事件绑定来附加事件处理程序。为此我建议你使用jquery,这个链接解释了Event binding on dynamically created elements

  3. 的方式
  4. 制作ajax请求jquery是非常好的库,但是如果你想使用XMLHttpRequest对象,那么检查响应的状态是个好主意:

    if(xmlhttp.status == 200){     的document.getElementById( “国家”)的innerHTML = xmlhttp.responseText。 }其他{      //处理错误; }