Use AJAX in PHP

Introduction of AJAX in PHP

We use AJAX in PHP. Asynchronous JavaScript and XML (AJAX) is a technique for creating fast and dynamic web pages. AJAX allows web pages to be updated asynchronously by exchanging small amounts of data with the server behind the scenes. This means that it is possible to update parts of a web page, without reloading the entire page. AJAX is based on internet standards, and uses a combination of:

  • XML Http Request objects (to exchange data asynchronously with a server).
  • JavaScript/DOM (to display/interact with the information).
  • CSS (cascaded style sheet data).
  • XML (often used as the format for transferring data).

Examle

//start here HTML code//

 

<html>

<head>

<script>

    function showHint(str) {

        if (str.length == 0) {

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

            return;

        }

        if (window.XMLHttpRequest) {

            xmlhttp = new XMLHttpRequest();

        }

        else {

            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

        }

        xmlhttp.onreadystatechange = function () {

            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

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

            }

        }

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

        xmlhttp.send();

    }

</script>

</head>

<body>

<div align="center">

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

<form>

 

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

 

</form>

 

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

</div>

</body>

</html

 

Note: If the input field is not empty, the showHint() function executes the following:

  • Create an XML Http Request object.

  • Create the function to be executed when the server response is ready.

  • Send the request off to a file on the server.

  • Notice that a parameter (q) is added to the URL (with the content of the input field).

//start here PHP code//

 

<?php

// Fill up array with names

$a[]="Anna";$a[]="Big boss";$a[]="Chandan";$a[]="Dogy";$a[]="Elegabeth";$a[]="Feena";$a[]="Grand father";$a[]="Heena";$a[]="India";$a[]="Jony";$a[]="King khan";$a[]="Linda";

$a[]="Nina";$a[]="Ompuri";$a[]="Petunia";$a[]="Ameesha";$a[]="Raquel";$a[]="Cindy";$a[]="Doris";$a[]="Eve";$a[]="Evita";$a[]="Sunniva";$a[]="Tove";$a[]="Unni";$a[]="Violet";

$a[]="Liza";$a[]="sharad";$a[]="vinod kumar";$a[]="manish sir";$a[]="Vicky";

 

//get the q parameter from URL

$q=$_GET["q"];

 

//lookup all hints from array if length of q>0

if (strlen($q) > 0)

  {

  $hint="";

  for($i=0; $i<count($a); $i++)

    {

    if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))

      {

      if ($hint=="")

        {

        $hint=$a[$i];

        }

      else

        {

        $hint=$hint." , ".$a[$i];

        }

      }

    }

  }

 

// Set output to "no suggestion" if no hint were found

// or to the correct values

if ($hint == "")

  {

  $response="no suggestion";

  }

elses

  {

  $response=$hint;

  }

 

//output the response

echo $response;

?>

 

Explanation: If there is any text sent from the JavaScript (strlen($q) > 0), then the following happens:

  • Find a name matching the characters sent from the JavaScript.

  • If no match is found then set the response string to "no suggestion".

  • If one or more matching names were found, set the response string to all these names.

  • The response is sent to the "txtHint" placeholder.

Output:

ajax1 use in php.jpg


ajax2 use in php.jpg
ajax3 use in php.jpg


Similar Articles