该错误是在ajax部分 document.getElementById(“ bookid”)。innerHTML上完成的 ,必须替换为
document.getElementById()。value, 因为我必须将包含值的 HTML元素 放入 HTML元素
,即Textbox(因为文本框包含value属性) 。
InnerHTML 用于处理不包含 value , div,h1,等的 html元素 。有关详细信息,请参见下面的链接。
http://www.verious.com/qa/what-39-s-the-difference-between-document-get-
element-by-id-quot-test-quot-value-and-document-get-element- by-id-quots
/
阿贾克斯代码
function show_bookid(str){ var xmlhttp; if(str.length==0) { document.getElementById("bookid").value=""; return; } if(window.XMLHttpRequest) { xmlhttp= new XMLHttpRequest(); } else { xmlhttp=new ActiveXOjbject("Microsoft.XMLHttpRequest"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("bookid").value=xmlhttp.responseText; } } xmlhttp.open("GET","getbook.php?q="+str,true); xmlhttp.send();}getbook.php
<?php$b=$_GET['q'];include('includes/security.php'); include('includes/dbconnect.php'); $database=new MySQLDatabase();$sql="select * from tbl_bkcat where book_id='".$b."'";$result=mysql_query($sql);$row=mysql_fetch_array($result);echo $row['Book_id'];?>addbook.php
<form name="bookadd" action="" method="post"> <fieldset> <p><label>Subject</label> <select name="subject" onChange="show_bookid(this.value);"> <?php while($sel_rows=mysql_fetch_array($subresult)) { ?> <option value="<?php echo $sel_rows['book_id'];?>"> <?php echo $sel_rows['subject']?> </option> <?php } ?> </select> </p> <p> <label >Book ID</label> <input type="text" id="bookid" name="book"/> </p>


