我搜索了一个可能的答案,并让自己通过了一些帖子,包括 如何修复“未捕获的 TypeError:无法调用 null 的方法 'appendChild'”和在 websocket.send 期间接收“InvalidStateError:DOM 异常 11”,但它们似乎都没有解决我的问题。将数据从 html 表单保存到 mysql 数据库的保存按钮工作得非常好,直到页面开始出现两个错误
页面上的下拉菜单使用 ajax 从数据库中动态更新。它们仍然可以正常工作,并根据选择的选项自动更新,但是当我单击任何下拉菜单时,错误控制台会
"Uncaught error: InvalidStateError: DOM Exception 11 on createoptions.js line 37
显示xmlhttp.send();
当我单击保存按钮时,会显示一条错误消息,而不是任何响应或将数据保存到数据库
Uncaught TypeError: Cannnot call method "submit" of null
。需要注意的是,调用 submit() 方法的对象已经存在。我使用document.getElementById('studentdata').submit()"
并且表格studentdata存在于同一页面上。
请强调这些错误的原因以及我能做些什么来解决它们。以下是相关代码。
创建选项.js
function setdepartment(value){
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("departmentselect1").innerHTML=xmlhttp.responseText;
}
}
switch(value){
case "Allied Health Sciences" : xmlhttp.open("GET","deptahs.php",true); break;
case "Engineering and Inter-disciplinary sciences" : xmlhttp.open("GET","depteids.php",true); break;
case "HIMSR" : xmlhttp.open("GET","depthimsr.php",true); break;
case "Islamic Studies and Social Sciences" : xmlhttp.open("GET","deptisss.php",true); break;
case "Management and Information Technology" : xmlhttp.open("GET","deptmanagement.php",true); break;
case "Medicine (Unani)" : xmlhttp.open("GET","deptmedicine.php",true); break;
case "Nursing" : xmlhttp.open("GET","deptnursing.php",true); break;
case "Pharmacy" : xmlhttp.open("GET","deptpharmacy.php",true); break;
case "Science" : xmlhttp.open("GET","deptscience.php",true); break;
}
xmlhttp.send();
}
page.php 和被引用的行是<li><a class="button black" form="studentdata" onclick="document.getElementById('studentdata').submit()">Save</a></li>
<div class="navbutton">
<aside>
<ul>
<li><a class="button black" href="logout.php">Logout</a></li>
<li><a class="button black" onclick="addRow()">Add Row</a></li>
<li><a href="#searchbox" class="button black" onclick="showsearch()">Search</a></li>
<li><a href="#promotediv" class="button black" onclick="showpromote()">Promote</a></li>
<li><a class="button black" form="studentdata" onclick="document.getElementById('studentdata').submit()">Save</a></li>
</ul>
</aside>
</div>
<form id="studentdata" action="savedata.php" method="POST">
<div style=" padding-left:350px; ">
<div class="dropdown dropdown-dark">
<select name="facultyselect1" id="facultyselect1" style="width:300px;" class="dropdown-select " onfocus="setdepartment(this.value)" onchange="setdepartment(this.value)" required>
<option value="">Select an option</option>
<div id="facultyselect1">
<?php
@ $db = mysql_connect("localhost", "root", "");
mysql_select_db("university");
$strSQL = "SELECT facultyname FROM faculty";
$rs = mysql_query($strSQL);
$nr = mysql_num_rows($rs);
for ($i=0; $i<$nr; $i++) {
$r = mysql_fetch_array($rs);
echo "<OPTION VALUE=\"".$r["facultyname"]."\">".$r["facultyname"]."</OPTION>";
}
?>
</div>
</SELECT>
</div>
<div class="dropdown dropdown-dark">
<select name="departmentselect1" id="departmentselect1" class="dropdown-select" onchange="setcourse(this.value)" onfocus="setcourse(this.value)" required>
<option value="">Select an option</option>
<div id="departmentselect1">
</div>
</select>
</div>
</div>
</form>