WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

AJAX PHP Example


AJAX is utilized to make progressively intelligent applications.


AJAX PHP Examples

The following precedent will show how a website page can speak with a web server while a client type characters in an info field:

Example

Start composing a name in the info :

First name:

Suggestions:



Example Explained

In the model above, when a client types a character in the info field, a capacity called "showHint()" is executed.

The work is activated by the onkeyup event.

Here is the HTML code:

Example

<html>
<head>
<script>
function showHint(str) {
    on the off chance that (str.length == 0) {
        document.getElementById("txtHint").innerHTML = "";
        return;
    } else {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = work() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
            }
        };
        xmlhttp.open("GET", "gethint.php?q=" + str, true);
        xmlhttp.send();
    }
}
</script>
</head>
<body>

<p><b>Start composing a name in the input field below:</b></p>
<form>
First name: <input type="text" onkeyup="showHint(this.value)">
</form>
<p>Suggestions: <span id="txtHint"></span></p>
</body>
</html>

Code explanation:

First, check if the info field is vacant (str.length == 0). In the event that it is, clear the substance of the txtHint placeholder and leave the function.

However, if the info field isn't unfilled, do the following:

  • Create a XMLHttpRequest object
  • Create the capacity to be executed when the server reaction is ready
  • Send the demand off to a PHP document (gethint.php) on the server
  • Notice that q parameter is included gethint.php?q="+str
  • The str variable holds the substance of the information field

The PHP File - "gethint.php"

The PHP document checks a variety of names, and returns the relating name(s) to the browser:

<?php
/Array with names
$a[] = "Eva";
$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 = "";

// query all indications from exhibit if $q is not the same as ""
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 clue was found or on the other hand yield right qualities
reverberation $hint === "" ? "no suggestion" : $hint;
?>