Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法在ajax中加载php中的数据库值_Javascript_Php_Ajax - Fatal编程技术网

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

Javascript 无法在ajax中加载php中的数据库值,javascript,php,ajax,Javascript,Php,Ajax,我是PHP的入门级开发人员,对PHP一无所知,我试图在选择下拉菜单时调用数据库值,到目前为止,我在教程的帮助下完成了这项工作,。。问题是,当我选择第一个下拉菜单时,第二个下拉菜单是从DB中获取值,但当我从第二个下拉菜单中选择option时,我无法获取页面上显示的任何数据。从第二个下拉列表中选择选项后,不会发生任何情况 <?php include '../connect.php'; $unm=htmlentities($_SESSION['username']); ?> <h

我是PHP的入门级开发人员,对PHP一无所知,我试图在选择下拉菜单时调用数据库值,到目前为止,我在教程的帮助下完成了这项工作,。。问题是,当我选择第一个下拉菜单时,第二个下拉菜单是从DB中获取值,但当我从第二个下拉菜单中选择option时,我无法获取页面上显示的任何数据。从第二个下拉列表中选择选项后,不会发生任何情况

<?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

ajax2.PHP


我建议你检查三件事

在id为state的div内部的select中,有一个onChange调用javascript函数“display_details”,用于加载包含详细数据的table元素。但是当您从AJAX.PHP返回select时,onchange事件有另一个函数“change\u test”,它应该是“display\u details”

在文档中动态插入新的html元素时,通常必须使用动态事件绑定来附加事件处理程序。为此,我建议您使用jquery,这个链接解释了如何使用jquery

对于ajax请求,jquery是一个非常好的库,但是如果您想使用XMLHttpRequest对象,最好检查响应的状态:

如果xmlhttp.status==200{ document.getElementByIdstate.innerHTML=xmlhttp.responseText; }否则{ //处理错误; }


在为第二个下拉菜单拉取数据时,您应该将详细信息更改为我认为testid,您指的是一个空的select选项,并且从ajax.php加载带有select的testid的选项。我真的不知道你在做什么谢谢你的回复,我今天会试试并回复谢谢你,成功了。。。我刚刚在ajax2.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>";
}
?>
<?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>";
}
    ?>