Php 在文本框中输出
我目前正在使用两个页面,一个是Main.php,另一个是php代码ajaxModule.php,以使我的AJAX工作。我还使用了一个SQL数据库,我用它来检索数据,以填充选择下拉列表等。我有一个文本框,用户在其中输入模块代码,在它下面的文本框中,模块名称将自动填写,因此用户不必写出来。我可以使用下面的AJAX函数来检索正确的模块名,但我无法将其输出到文本框中,相反,我发现唯一有效的方法是span。我意识到文本框只用于输入,但我已经找到了在文本框中输出的方法,但无法让它们工作。我还尝试将从页面中的PHP获取的值数组(其中包含AJAX)传递回Main.PHP,以便以这种方式输出,但我似乎也无法使其正常工作 以下是我的Main.php页面中需要的代码:Php 在文本框中输出,php,sql,ajax,textbox,get,Php,Sql,Ajax,Textbox,Get,我目前正在使用两个页面,一个是Main.php,另一个是php代码ajaxModule.php,以使我的AJAX工作。我还使用了一个SQL数据库,我用它来检索数据,以填充选择下拉列表等。我有一个文本框,用户在其中输入模块代码,在它下面的文本框中,模块名称将自动填写,因此用户不必写出来。我可以使用下面的AJAX函数来检索正确的模块名,但我无法将其输出到文本框中,相反,我发现唯一有效的方法是span。我意识到文本框只用于输入,但我已经找到了在文本框中输出的方法,但无法让它们工作。我还尝试将从页面中的
<div id="slide3" class = "unactiveSlide">
<form id = "requestForm">
<div id = "requestdiv">
<table>
<tr><!-- This is where the user inputs the module code and the function is called with onkeyup-->
<td><a href="#" title="Enter the module code e.g. 'COB290' i.e. Department Code followed by Part and Actual Module Code">Module Code</a></td>
<?php
echo "<td>";
echo "<input type='text' size='40' id='moduleCodeID' onkeyup='loadXMLDoc(this.value)'/>";
echo "</td>";
?>
</tr>
<tr><!-- This is where the module name appears with the use of AJAX-->
<td><a href="#" title="The name of the module e.g. 'Team Projects'">Module Name</a></td>
<?php
<td><span id='moduleCodeToNameDiv'></span></td>
</tr>
</table>
</div>
</form>
</div>
下面是代码上方的AJAX函数,也是Main中的函数。php q是我在下一页中用于传递值的变量:
<head>
<script>
//AJAX SCRIPT FOR MODULE CODE TO MODULE NAME
function loadXMLDoc(str)
{
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("moduleCodeToNameDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","ajaxModule.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
以下是我的名为ajaxModule.php的AJAX文件中的代码:
<html>
<body>
<?php
$q = $_GET["q"];
$con = mysqli_connect('localhost','root','','team06');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"reqs");
$sql = "SELECT Module_Title FROM module WHERE Module_Code = '" . $q."'";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result))
{
echo $row['Module_Title'];
}
?>
</body>
</html>
谁能告诉我如何把它输出到文本框中?
提前感谢,
Idris。要设置值,请不要指定给innerHTML:
别忘了逃避$q嗨,谢谢你的回复。我只是研究了innerHTML,但我不太明白你给我的代码放在哪里了。你能告诉我在哪里用吗。提前谢谢。那么现在把它放在moduleCodeToNameDiv里会很累吗?将该ID替换为输入元素ID,并将.innerHTML替换为.value
Document.getElementById("xxx").value = xmlhttp.responseText;