Communicate a Web Page into a Web Server in PHP and AJAX

0
52

This tutorial is all about Communicate a Web Page into a Web Server in PHP and AJAX.
Good day everyone! Another tutorial for all programmers especially in the field of web development. This tutorial is about creating interactive web pages. With the use of PHP and AJAX we will make it possible.

So now, we will create a web page and make a simple scripts for communicating that page into a web server while typing in a input field.

To be able for you to understand about how this happen. Let me discuss you how it works.

When the user types a character in the input field, a function that we will make named “showHint()” will be executed. This function would be triggered by the onkeyup event.

Below is the PHP code:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>PHP - AJAX</title>
  6.  
  7. function showHint(str) {
  8. if (str.length === 0) {
  9. document.getElementById("txtHint").innerHTML = "";
  10. return;
  11. } else {
  12. var xmlhttp = new XMLHttpRequest();
  13. xmlhttp.onreadystatechange = function() {
  14. if (this.readyState === 4 && this.status === 200) {
  15. document.getElementById("txtHint").innerHTML = this.responseText;
  16. }
  17. };
  18. xmlhttp.open("GET", "gethint.php?q=" + str, true);
  19. xmlhttp.send();
  20. }
  21. }
  22.  
  23. </head>
  24. <body>
  25. <p><b>Type a name below.</b></p>
  26. <form>
  27. First Name: <input type="text" onkeyup="showHint(this.Value)" />
  28. </form>
  29. <p>Suggestions: <span id="txtHint"></span></p>
  30. </body>
  31. </html>

So now, I will explains how does the codes above goes:

So first, the code (str.lenght == 0) check if the input field is empty. Then if it is empty, the content of the txtHint placeholder should be clear and the function “showHint()” will be exit.

However, if the input field is not empty, do the following:

  • Create an XMLHttpRequest object
  • Create the function to be executed when the server response ready
  • Send the request off to a PHP file (gethint.php) on the server
  • Notice the q parameter is added to the url (gethint.php?q=”+str)
  • And the str variables holds the content of the input field

Now, create a “gethint.php” file then put the following codes.

  1. <?php
  2. //array with names
  3. $a[] = "Harley";
  4. $a[] = "Joken";
  5. $a[] = "Janobe";
  6. $a[] = "Christian";
  7. $a[] = "Diosher";
  8. $a[] = "Warren";
  9. $a[] = "Paolo";
  10. $a[] = "Jay";
  11. $a[] = "Joshua";
  12. $a[] = "Stephen";
  13.  
  14. //get the q parameter from url
  15. $q = $_REQUEST["q"];
  16.  
  17. $hint = "";
  18.  
  19. //lookup all hints from array if $q is different from ""
  20.  
  21. if ($q !== "") {
  22. $q = strtolower($q);
  23. $len = strlen($q);
  24.  
  25. foreach($a as $name) {
  26. if (stristr($q, substr($name, 0,$len))) {
  27. if ($hint === "") {
  28. $hint = $name;
  29. } else {
  30. $hint .= ", $name";
  31. }
  32. }
  33. }
  34. }
  35.  
  36. //Ouput "no suggestion" if no hint was found or output corrent values
  37. echo $hint === "" ? "no suggestion" : $hint;
  38.  
  39. ?>

The codes above checks an array of names, and returns the corresponding name(s) to the browser.

Done.

If you have question regarding this article entitled as “COMMUNICATE A WEB PAGE INTO A WEB SERVER in PHP and AJAX” feel free to ask us by commenting below or visit on our contact page.

 

Other articles you might read also:

 

 

 

 

Facebook Comments
(Visited 42 times, 1 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here