2015-02-10 181 views
0

我目前使用两个页面,一个是我的Main.php,另一个是为了让我的AJAX工作的PHP代码(ajaxModule.php)。我还使用了我用来检索数据以填充选择下拉列表等的SQL数据库。我有一个文本框,用户可以在其中输入模块代码和其下方的文本框,模块名称将自动填写,因此用户不必将其写出。我能够使用下面的AJAX函数来检索正确的模块名称,但我无法将其输出到文本框中,而是我发现的唯一一件作品是跨度。我意识到一个文本框只是用于输入,但我已经找到了在文本框中输出的方法,但无法让它们工作。我也试图将我从页面中的PHP获得的数组值传递给Main.php,以便以这种方式输出,但是我似乎无法使其工作。在文本框中输出

这里是你从我Main.php页面需要的代码:

<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> 

这里是我的AJAX文件中的代码名为ajaxModule.php:

<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。

回答

0

要设置<input>值不分配给innerHTML代替:

Document.getElementById("xxx").value = xmlhttp.responseText; 

不要忘了逃跑$q

+0

您好,感谢您的答复。我刚刚研究了innerHTML,但我不太明白我将这些代码放在哪里。你能告诉我我在哪里使用它。提前致谢。 – IdrisAH 2015-02-10 16:05:25

+0

那么现在轮胎把它放在'moduleCodeToNameDiv'中?将该ID替换为输入元素ID,并替换'.value'的'.innerHTML' – 2015-02-10 19:03:17