我现在遇到了更大的问题。每当我想选择摄入量和受试者时,它将分别显示未定义的索引:摄入量和未定义的索引:程序。即使我的数据库包含数据,当我选择其他选择时,它也没有显示主题的选项。我的代码无法检索它还是其他内容?Result Image: Errors需要帮助,谢谢
<?php
include "..\subjects\connect3.php";
//echo "Connection successs";
$query = "SELECT * FROM programmes_list";
$result = mysqli_query($link, $query);
?>
<form name = "form1" action="" method="post">
<table>
<tr>
<td>Select Pragramme</td>
<td><select id="programmedd" onChange="change_programme()">
<option>select</option>
<?php
while($row=mysqli_fetch_array($result)){
?>
<option value="<?php echo $row["ID"]; ?>"><?php echo $row["programme_name"]; ?></option>
<?php
}
?>
</select></td>
</tr>
<tr>
<td>Select intake</td>
<td>
<div id="intake">
<select>
<option>Select</option>
</select>
</div>
</td>
</tr>
<tr>
<td>Select Subjects</td>
<td>
<div id="subject">
<select>
<option>Select</option>
</select>
</div>
</td>
</tr>
</table>
</form>
<script type="text/javascript">
function change_programme()
{
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","ajax.php?programme="+document.getElementById("programmedd").value,false);
xmlhttp.send(null);
document.getElementById("intake").innerHTML=xmlhttp.responseText;
}
function change_intake()
{
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","ajax.php?intake="+document.getElementById("intakedd").value,false);
xmlhttp.send(null);
document.getElementById("subject").innerHTML=xmlhttp.responseText;
}
</script>
//ajax.php
<?php
$dbhost = 'localhost' ;
$username = 'root' ;
$password = '' ;
$db = 'programmes' ;
$link = mysqli_connect("$dbhost", "$username", "$password");
mysqli_select_db($link, $db);
$programme=$_GET["programme"];
$intake=$_GET["intake"];
if ($programme!="")
{
$res=mysqli_query($link, "select * from intakes where intake_no = $programme");
echo "<select id='intakedd' onChange='change_intake()'>";
while($value = mysqli_fetch_assoc($res))
{
echo "<option value=".$value['ID'].">";
echo $value["intake_list"];
echo "</option>";
}
echo "</select>";
}
if ($intake!="")
{
$res=mysqli_query($link, "select * from subject_list where subject_no = $intake");
echo "<select>";
while($value = mysqli_fetch_assoc($res))
{
echo "<option value=".$value['ID'].">";
echo $value["subjects"];
echo "</option>";
}
echo "</select>";
}
?>
答案 0 :(得分:1)
您获取未定义索引通知的原因是change_programme()
仅在调用ajax.php时发送?programme=
,{{1} } 仅在通话中发送change_intake()
。但是,在ajax.php中,您正试图从?intake=
获取两者。
因此,来自$_GET
的调用会为change_programme()
提供未定义的索引通知,因为它实际上并未在URL中提供。由于同样的原因,$_GET['intake']
的调用会为change_intake()
提供未定义的索引通知。
您可以通过检查是否已设置来解决此问题:
$_GET['programme']
这是:
的简写$programme = isset($_GET["programme"]) ? $_GET["programme"] : "";
$intake = isset($_GET["intake"]) ? $_GET["intake"] : "";
作为旁注,从不信任您从网址获得的任何输入,尤其是,如果您要在查询中直接使用它。 请首先通过请使用绑定参数的prepared statement来减少SQL注入攻击的可能性if (isset($_GET["programme"])) {
$programme = $_GET["programme"];
} else {
$programme = "";
}
if (isset($_GET["intake"])) {
$intake = $_GET["intake"];
} else {
$intake = "";
}
传递$programme
和$intake
变量