2017-04-02 5 views
0

私はPHPのエントリーレベルの開発者です.PHPには知識がありません。私はドロップダウンメニューを選択する際にデータベース値を呼び出そうとしています。チュートリアル、..問題は、2番目のドロップダウンを選択するとDBから値が取得されますが、2番目のドロップダウンメニューからオプションを選択すると、ページに何も表示されません。 2番目のドロップダウンからオプションを選択しても何も起こりません。ajaxのPHPでデータベース値をロードできません

<?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>"; 
} 
    ?> 
+0

あなたは、私が2番目のドロップダウンのためのデータを引っ張ったときに 'testid'、あなたは空の選択オプションを参照している、とあなたajax.phpからあなたがロードしていると思うに' details'を変更する必要がありますselectの 'testid' idを持つオプションあなたがやっていることは本当にわからない – Roljhon

答えて

0

私は3つのことを確認するためにあなたをお勧めします。 IDの#stateと内側のdivにあるという選択で

  1. 、あなたが詳細データをテーブル要素をロードするために使用するjavascript関数呼び出しのonChange「display_detailsを()」を持ちます。しかし、AJAX.PHPからselectを返すと、onchangeイベントに別の関数 'change_test()'があります。これは 'display_details()'でなければなりません。

  2. 動的に新しいhtml要素をドキュメントに挿入すると、動的イベントバインディングを使用してイベントハンドラをアタッチします。私はあなたがjqueryのを使用することをお勧めします。このためにの状態を確認し、このリンクは非常に良いライブラリがあるEvent binding on dynamically created elements

  3. は、AJAX要求jQueryのに作成する方法について説明していますが、XMLHttpRequestオブジェクトを使用したい場合、それは良いアイデアになります応答:

    IF(xmlhttp.statusの== 200){ のdocument.getElementById( "状態")のinnerHTML = xmlhttp.responseText。 } else { //エラーを処理します。 }

+0

返事ありがとう、私は今日これを試して返信します。 :) –

+0

ありがとう、それは働いた...私はちょうどajax2.phpの変更 -

関連する問題