我從來沒有使用Ajax之前,我玩了一些試圖學習它(我喜歡理解的東西,只是像jquery像一個同事建議之前)。我有一個以單選按鈕開始的頁面(選自數據庫的選項),該選項應該觸發新的單選按鈕(同樣,選項取自數據庫)。我首先簡單地展示了最初的'選擇'的結果,但是當我從單選按鈕中選擇一些內容時,沒有任何反應,但我不明白爲什麼。如果有人能夠告訴我爲什麼這不能達到我期望的效果,我會很喜歡它。使用Ajax有一組數據庫派生的單選按鈕
在此先感謝
主頁:
<html>
<head>
<script>
function showGlycopeptides(str)
{
var xmlhttp;
if (str=="")
{
document.getElementById("txtField").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtField").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getglycopeptides.php?q="+str,true);
xmlhttp.send();
}
</script>
<title>LeidenGlycoPeptide DataBase</title>
</head>
<body>
<h1>Welcome to the LeidenGlycoPeptide DataBase</h1>
<?php
$link = mysql_connect("localhost","reader","") or die (mysql_error());
mysql_select_db('leidenGlycoPeptide') or die();
$query = 'select protein from glycoPeptide';
$result = mysql_query($query);
mysql_close($link);
?>
<form>
<p>Select glycopeptide to search for (interactive dialog)</p>
<?php
echo"<select name=\"prec\" onchange=\"showGlycopeptides(this.value)\">";
echo"<option value=\"\">select glycoprotein</option>";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
foreach ($line as $col_value) {
echo"<option value=\"$col_value\">$col_value</option>";
}
}
echo"</select>";
?>
</form>
<br>
<div id="txtField"><b>Text field</b></div>
</body>
getglycopeptides.php:
<html>
<head>
<title>glyco</title>
</head>
<body>
<?php
$q=$_GET["q"];
$link = mysql_connect("localhost","reader","") or die (mysql_error());
mysql_select_db("leidenGlycoPeptide",$link) or die();
$query = "select glycoType from run,glycoPeptide where run.id = glycoPeptide.id and glycoPeptide.protein like '".$q."'";
echo "<select name=\"type\" onchange=\"foo\">";
echo "<option value=\"\">select glycosylation</option>";
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
foreach ($row as $col_value)
{
echo"<option value=\"$col_value\">$col_value</option>";
}
}
echo "</select>";
$result = mysql_query($query);
mysql_close($link);
?>
</body>
</html>
---- ---- EDITED
的代碼是編輯,以便它們可以作爲示例(用於其他人)。
哦,還有一件事我剛剛發現。你需要定義xmlhttp變量,而你沒有。只需在showGlycopeptides功能的開始處添加'var xmlhttp;' –
我喜歡Firebug的控制檯部分,給你一個upvote並接受這個答案。我設法發現了造成這種失敗的(非常愚蠢的)錯誤。 –