0

我将输入发送到 php 服务器,服务器将输出返回到客户端。似乎输入不会发送到服务器,并且我在实现高亮功能的逻辑上遇到了一些问题。

客户端键入输入并发送到服务器

  <html>
  <head>
  <script type="text/javascript">
  function showUser(str)
  {
  if (str=="")
  {
     document.getElementById("txtHint").innerHTML="";
     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("txtHint").innerHTML=xmlhttp.responseText;
  }
}
   xmlhttp.open("GET","test_server.php?q="+str,true);
   xmlhttp.send();
}
</script>
</head>
<body>

<form>
    Input: <input type="text" name="user" />
   <input type="submit" value="Submit" />
 </form>
<br />
<div id="txtHint"><b>The result will be listed here.</b></div>

</body>
</html>

服务器获取输入以及句子中是否有名称或数字。它突出显示这些条件并返回给客户。

 <?php
 $q=$_GET["q"];
 $name=array("jake","jill");
 $number=array("one","two","three","four","five");
 $sentence= split(" ", $q);
 echo "<table border='1'>
 <tr>
 <th>insert</th>
 <th>tagged</th>

 </tr>";
 echo "<tr>";
 echo "<td>" . $q . "</td>";
 echo "<td>" "</td>";
 echo "</tr>";

 echo "</table>";

 ?>
4

1 回答 1

1

试试这个,在这里工作得很好:

 <html>
  <head>
  <script type="text/javascript">
window.onload = function(){
  function showUser(str)
  {
  if (str=="")
  {
     document.getElementById("txtHint").innerHTML="";
     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("txtHint").innerHTML=xmlhttp.responseText;
  }
}
   xmlhttp.open("GET","test_server.php?q="+str,true);
   xmlhttp.send();
   return false;
}
document.getElementById('form').onsubmit = function(){
    showUser(document.getElementById('user').value);
    return false;
}
}
</script>
</head>
<body>

<form id="form">
    Input: <input type="text" id="user" name="user" />
   <input type="submit" value="Submit" />
 </form>
<br />
<div id="txtHint"><b>The result will be listed here.</b></div>

</body>
</html>
于 2012-05-08T00:04:06.617 回答