-1

I am sending input to php server and the server returns a output to the client side. It seems like input is not going to the server and i have some problem with logic to implement highlight function.

Client side you type input and send to the server

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

the server gets input and if there is name or number in a sentence. It highlights those conditions and return to the client.

 <?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
  • 2
    Just a tip... while it is entirely possible to use XmlHttpRequest and what not to get this job done, there are many libraries that make the process much easier. I recommend jQuery for this kind of thing, and it provides quite a bit of functionality for manipulating the DOM as well. Again, it isn't 100% necessary, but will save you some hassle. Commented May 7, 2012 at 23:57
  • I agree with @Brad, jQuery can save quite a lot of hassle. Commented May 8, 2012 at 0:09
  • can you give me simple code for jquery? Commented May 8, 2012 at 0:19
  • You can google for jQuery example code. It's not overly hard. Commented May 8, 2012 at 0:29

1 Answer 1

0

Try this, worked well here:

 <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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.