加载在一个文本框,从数据库中的数据与PHP和AJAX纯 [英] Loading data from database with php and pure ajax in a textbox
问题描述
我有一个下拉列表中有数据从数据库加载科目。当我点击一个主题,它应该做的是负载相关的subject_id里面的值文本框,仅略低于下拉列表选项。我不知道如何从getbook.php并显示在book_ID输入文本带来的价值。
show_bookid(STR){
VAR XMLHTTP;
如果(str.length == 0){
。的document.getElementById(BOOKID)的innerHTML =;
返回;
}
如果(window.XMLHtt prequest){
XMLHTTP =新XMLHtt prequest();
} 其他 {
强大的文本
XMLHTTP =新ActiveXOjbject(Microsoft.XMLHtt prequest);
}
xmlhttp.onreadystatechange =功能(){
如果(xmlhttp.readyState == 4和&安培; xmlhttp.status == 200){
的document.getElementById(BOOKID)的innerHTML = xmlhttp.responseText。
}
}
xmlhttp.open(GET,getbook.php Q =?+ STR,真正的);
xmlhttp.send();
}
getbook.php
< PHP
< PHP
$ B = $ _GET ['Q'];
包括('包括/ security.php');
包括('包括/ dbconnect.php');
$数据库=新MySQLDatabase();
$ SQL =SELECT * FROM tbl_bkcat那里book_id ='。 $ B。 ';
$结果= mysql_query($ SQL);
?>
?>
下面是我需要带来价值的文件
<表格名称=bookadd行动=级=jNice方法=邮报>
&其中p为H.;
<标签>受试对象; /标签>
<选择名称=主题的onChange =show_bookid(THIS.VALUE);>
<?PHP而($ sel_rows = mysql_fetch_array($子结果)){>
<期权价值=< PHP的echo $ sel_rows ['book_id'];?>>
<?PHP的echo $ sel_rows ['主题']>
< /选项>
< PHP}&GT?;
< /选择>
&所述; / P>
&其中p为H.;
<标签> Book_Id< /标签>
<输入类型=文本ID =BOOKID级=文小/> //,我需要加载主题ID< / P>
这个错误是在做ajax的一部分的document.getElementById(BOOKID)。innerHTML的并必须替换文档.getElementById()。值,因为我不得不把数据为 HTML元素的cotain值即文本框(如文本框包含值的属性)。
的innerHTML 被用来操纵在 HTML元素不包含值 **格,H1,**等详细内容见下面的链接。
<一个href="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-quot-tes/" rel="nofollow">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-quot-tes/
阿贾克斯code
函数show_bookid(STR)
{
VAR XMLHTTP;
如果(str.length == 0)
{
。的document.getElementById(BOOKID)值=;
返回;
}
如果(window.XMLHtt prequest)
{
XMLHTTP =新XMLHtt prequest();
}
其他
{
XMLHTTP =新ActiveXOjbject(Microsoft.XMLHtt prequest);
}
xmlhttp.onreadystatechange =功能()
{
如果(xmlhttp.readyState == 4和&安培; xmlhttp.status == 200)
{
。的document.getElementById(BOOKID)值= xmlhttp.responseText;
}
}
xmlhttp.open(GET,getbook.php Q =?+ STR,真正的);
xmlhttp.send();
}
getbook.php
&LT; PHP
$ B = $ _ GET ['Q'];
包括('包括/ security.php');
包括('包括/ dbconnect.php');
$数据库=新MySQLDatabase();
$ SQL =SELECT * FROM tbl_bkcat那里book_id ='$ B。';
$结果= mysql_query($ SQL);
$行= mysql_fetch_array($结果);
回声$行['Book_id'];
?&GT;
addbook.php
&LT;表格名称=bookadd行动=级=jNice方法=邮报&GT;
&LT;字段集&GT;
&LT; P&GT;&LT;标签&gt;受试对象; /标签&gt;
&LT;选择名称=主题的onChange =show_bookid(THIS.VALUE);&GT;
&LT; PHP
而($ sel_rows = mysql_fetch_array($子结果))
{
?&GT;
&LT;期权价值=&LT; PHP的echo $ sel_rows ['book_id'];?&GT;&GT;
&LT;?PHP的echo $ sel_rows ['主题']&GT;
&LT; /选项&GT;
&LT; PHP
}
?&GT;
&LT; /选择&GT;
&所述; / P&GT;
&其中p为H.;
&LT;标签&gt;图书ID&LT; /标签&gt;
&LT;输入类型=文本ID =BOOKIDNAME =书/&GT;
&所述; / P&GT;
i have one dropdown list which has data for "subjects" loaded from database. when i clicked on one subject what it should do is load related "subject_id" value inside textbox which is just below dropdown list option. i dont know how to bring value from getbook.php and show in book_ID input text.
show_bookid(str) {
var xmlhttp;
if (str.length == 0) {
document.getElementById("bookid").innerHTML = "";
return;
}
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
strong text
xmlhttp = new ActiveXOjbject("Microsoft.XMLHttpRequest");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("bookid").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "getbook.php?q=" + str, true);
xmlhttp.send();
}
getbook.php
<?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);
?>
?>
below is the File where i need to bring value
<form name="bookadd" action="" class="jNice" method="post">
<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" class="text-small" />//where i need to load subject id</p>
The mistake was done at ajax part document.getElementById("bookid").innerHTML and must be replaced with document.getElementById().value since I had to put data to Html Element that cotain value i.e Textbox(as textbox contain value attribute).
InnerHTML is used to manipulate the html elements that does not contain value, ** div,h1, ** etc. for details see below link.
ajax code
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="" class="jNice" 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>
这篇关于加载在一个文本框,从数据库中的数据与PHP和AJAX纯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!