我制作了一个简单的 html 页面,它从我的一个数据库表中获取信息并将其粘贴到文本框中。它通过处理请求的 AJAX 函数连接到 PHP 来做到这一点。
我想知道的是是否可以将这些数据放入两个文本框中,而不仅仅是一个。我不知道该怎么做,因为在我的代码中我必须声明一个文本框,我是否必须为每个文本框创建单独的函数才能使其工作,还是有更简单的解决方案?
网页:
<html>
<head>
<script type="text/javascript">
function getDetails(str)
{
if (str=="")
{
document.getElementById("Text1").value="";
return;
}
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("Text1").value=xmlhttp.responseText; // here is why it only goes into "Text1"
}
}
xmlhttp.open("GET","getDetails.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<!--here a user enters an "RFID" and the details are returned into "Text1"-->
<input type="text" name="RFID1" value="" onKeyup="getDetails(this.value)" />
<input type="text" id="Text1" name="Text1" />
<input type="text" id="TextScore1" name="TextScore1"/>
</form>
<br/>
<form>
<!--here a user enters another "RFID" and the details are also returned into "Text1"
(though I would like it to go to Text2 and TextScore2)-->
<input type="text" name="RFID2" value="" onKeyup="getDetails(this.value)" />
<input type="text" id="Text2" name="Text2"/>
<input type="text" id="TextScore2" name="TextScore2"/>
</form>
</body>
</html>
PHP页面:
<?php
$q=$_GET["q"];
$con = mssql_connect("SQL", "0001", "Password");
if (!$con)
{
die('Could not connect: ' . mssql_get_last_message());
}
mssql_select_db("database1", $con);
$sql="SELECT * FROM Scrabble WHERE RFID = '".$q."'";
$result = mssql_query($sql);
while($row = mssql_fetch_array($result))
{
echo $row['Tile'];
echo $row['TileScore'];
}
mssql_close($con);
?>
*注意 - 我的服务器使用 MsSQL
另一个问题,正如您在 HTML 文件中看到的那样,我有两个表单,我需要相同的功能发生在两个表单上。在这里,我想我可能必须为每个表单创建另一个 PHP 文件来连接。但是为了确定我要问,是否可以将其保存在一个文件中,如果可以,您将如何处理?
编辑似乎我让一些人感到困惑,我不希望将文本放入两个文本框中,但实际上将结果拆分为两个文本框。这样“文本”将出现在文本框 Text1 中,而 TextScore 将出现在文本框 TextScore1 中