1

我制作了一个简单的 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 中

4

4 回答 4

1

为什么不是这个?

if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
  var response=xmlhttp.responseText;
  document.getElementById("Text1").value=response;
  document.getElementById("Text2").value=response;
}

对于第二个函数,您可以使用相同的 PHP 文件。在进行 ajax 调用以确定“此调用来自何处”时,您可能需要将查询字符串值传递给 PHP 页面。

就像是

getDetails.php?from="userinfo"

getDetails.php?from="checkprice"

在您的 PHP 文件中,您可以检查查询字符串变量的值并执行相应的代码集。您可以在那里使用 if /switch 等...

但我更愿意在逻辑上分离功能并将其保存在单独的文件中。

例如:我会将所有与用户相关的功能保存在 a 中userajaxhelper.php,并将与订单相关的功能保存在另一个名为 o 的文件中rderajaxhelper.php。这使我的代码井井有条且干净。

编辑:只想在我的回答中加入安迪休谟的评论,因为它很重要。你真的应该确保你不会成为SQL 注入的受害者。

于 2012-05-13T12:55:16.930 回答
0

你有一个代码线

document.getElementById("Text1").value=xmlhttp.responseText;

如果您想在 box2 中添加相同的文本

document.getElementById("yourSecondText").value=xmlhttp.responseText;

如果不一样,那么 php 应该以 JSON 格式给出答案,part1,.... 分开。

作为对 ajax 的响应,您应该为其文本框分配适当的部分。

对不起,如果我没有得到这个问题。

于 2012-05-13T13:00:33.583 回答
0

您可以将 PHP 的结果作为 JSON 返回,请参阅json_encode并发送正确的标头,内容类型为 application/json。

然后使用json2解析 JavaScript 中的 JSON,您将能够将一个键/参数分配给第一个字段,另一个分配给另一个字段。

...
$result = array();
while($row = mssql_fetch_array($result))
{
    $result[] = $row;
}
mssql_close($con);

header('Content-type: application/json');
echo json_encode($result);

和 JS(注意:我没有测试 JS 代码,但它应该提供一个示例):

...
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
    var jsonObj = JSON.parse(xmlhttp.responseText);

    document.getElementById("Text1").value = jsonObj.Title;
    document.getElementById("TextScore1").value = jsonObj.TileScore;
}
...
于 2012-05-13T13:08:24.937 回答
0

简短的回答:是的。

更长的答案:当响应返回时,您可以“随心所欲”。你只需要弄清楚你想要做什么以及如何实现它;较早的答案已经给出了很好的指导。但是,作为一般概念,您可能希望“缩减”到问题的准系统 - 让您拥有的页面尽可能通用,以便它仍然可以满足您的需求。

您可能想要做的示例(未经测试,只是快速设置,但我认为您应该做到这一点)......

<html>
    <head>
        <script type="text/javascript"><!--
            function doAjaxRequest() {
                // Set up Ajax-request, that will execute the
                // response it gets from the server "as is"...
                // Just what you were doing, but instead of
                // putting the response into textarea, execute it...
                eval(xmlhttp.responseText);
            }
        // --></script>
    </head>
    <body>
        <form>
            <textarea id="txt1"></textarea>
            <textarea id="txt2"></textarea>
            <input type="button" onclick="doAjaxRequest();return false">
        </form>
    </body>
</html>

而服务器端...

<?php
    // It doesn't matter which way you get the data, just... use it :-p
    echo('document.getElementById("txt1").value="Response 1";');
    echo('document.getElementById("txt2").value="Response 2";');
?>
于 2012-05-13T13:49:20.513 回答