🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to JavaScript Notes
Topic #260

AJAX PHP


AJAX is used to create more interactive applications.


AJAX PHP Example

The following example demonstrates how a web page can communicate with a web server while a user types characters in an input field:

Example

<p><b>Start typing a name in the input field below:</b></p>
<p>Suggestions: <span id="txtHint"></span></p>
<p>First name: <input id="txt1" onkeyup="showHint(this.value)" size="20" type="text"/></p>

Example Explained

In the example above, when a user types a character in the input field, a function called showHint() is executed.

The function is triggered by the onkeyup event.

Here is the code:

Example

<p>Start typing a name in the input field below:</p>

<p>Suggestions: <span id="txtHint"></span></p>

<form>

First name: <input type="text" onkeyup="showHint(this.value)">
</form>

<script>

function showHint(str) {

  if (str.length == 0) {

    document.getElementById("txtHint").innerHTML = "";

    return;

  } else {

    const xmlhttp = new XMLHttpRequest();

    xmlhttp.onload = function() {

      document.getElementById("txtHint").innerHTML = this.responseText;

    }

  xmlhttp.open("GET", "gethint.php?q=" + str);

  xmlhttp.send();

  }

}

</script>

Code explanation:

First, check if the input field is empty (str.length == 0). If it is, clear the content of the txtHint placeholder and exit the function.

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 is ready
  • Send the request off to a PHP file (gethint.php) on the server
  • Notice that q parameter is added gethint.php?q="+str
  • The str variable holds the content of the input field

The PHP File - "gethint.php"

The PHP file checks an array of names, and returns the corresponding name(s) to the browser:

<?php

// Array with names

$a[] = "Anna";

$a[] = "Brittany";

$a[] = "Cinderella";

$a[] = "Diana";

$a[] = "Eva";

$a[] = "Fiona";

$a[] = "Gunda";

$a[] = "Hege";

$a[] = "Inga";

$a[] = "Johanna";

$a[] = "Kitty";

$a[] = "Linda";

$a[] = "Nina";

$a[] = "Ophelia";

$a[] = "Petunia";

$a[] = "Amanda";

$a[] = "Raquel";

$a[] = "Cindy";

$a[] = "Doris";

$a[] = "Eve";

$a[] = "Evita";

$a[] = "Sunniva";

$a[] = "Tove";

$a[] = "Unni";

$a[] = "Violet";

$a[] = "Liza";

$a[] = "Elizabeth";

$a[] = "Ellen";

$a[] = "Wenche";

$a[] = "Vicky";

 // get the q parameter from URL
$q = $_REQUEST["q"];

$hint = "";

// lookup all hints from array if $q is different from ""
if ($q !== "") {
  $q = strtolower($q);

    $len=strlen($q);
  foreach($a as $name) {
    if (stristr($q, substr($name, 0, $len))) {
      if ($hint === "") {
        $hint = $name;
      } else {

          $hint .= ", $name";
      }

      }
  }
}

// Output "no suggestion" if no hint was found or output correct values

 echo $hint === "" ? "no suggestion" : $hint;
?>

Want to go beyond the notes?

Join CodingNow 2.0's JavaScript course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

AJAX PHP – FAQs

Quick answers about learning AJAX PHP in JavaScript.

This free note from CodingNow 2.0 explains AJAX PHP in JavaScript — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every JavaScript topic on CodingNow 2.0, including AJAX PHP, is 100% free with no signup required.
With focused practice, most students grasp AJAX PHP in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now