我将输入发送到 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>";
?>