How Can I Access Data From PHP Files Using JS AJAX

admin_img Posted By Bajarangi soft , Posted On 09-10-2020

In Java script we can access data from php file through ajax so today we discuss how to access data from php file

How Can I Access Data From PHP Files Using JS AJAX

Step 1:
Create index.html and implement below code.

<h2>Suggestions: <span id="demo"></span></h2>

<p>First name: <input class="form-control" type="text" id="txt1" onkeyup="showHint(this.value)"></p>


Step 2:
Create php file as getdata.php file and implement below code in it.
<?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;
?>


Step 3:
Now Implement java script to access php file data.
 

<script>
    function showHint(str) {
        var xhttp;
        if (str.length == 0) {
            document.getElementById("txtHint").innerHTML = "";
            return;
        }
        xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                document.getElementById("demo").innerHTML = this.responseText;
            }
        };
        xhttp.open("GET", "getdata.php?q="+str, true);
        xhttp.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


Complete Code For Accessing Data From PHP Files Using JS AJAX

<!DOCTYPE html>
<html>
<head>
    <title>How To Access Data From PHP File Using JS AJAX</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>

<body>
<div class="container">
    <br>
    <div class="text-center">
        <h1 id="color" style="color: tomato">How To Access Data From PHP File Using JS AJAX</h1>
    </div>
    <div class="well">
        <h2>Suggestions: <span id="demo"></span></h2>

        <p>First name: <input class="form-control" type="text" id="txt1" onkeyup="showHint(this.value)"></p>

        <script>
            function showHint(str) {
                var xhttp;
                if (str.length == 0) {
                    document.getElementById("txtHint").innerHTML = "";
                    return;
                }
                xhttp = new XMLHttpRequest();
                xhttp.onreadystatechange = function() {
                    if (this.readyState == 4 && this.status == 200) {
                        document.getElementById("demo").innerHTML = this.responseText;
                    }
                };
                xhttp.open("GET", "getdata.php?q="+str, true);
                xhttp.send();
            }
        </script>
    </div>
</div>
</body>
</html>

 

Related Post