Using AJAX to get data from a PHP array

I'm trying to use AJAX to help me retrieve data from my PHP array. I want to be able to enter a name, and get out the corresponding number. I've played around with W3schools AJAX PHP code, but don't know how to change it to give me the corresponding number I'm looking for? My array looks like this:

$a = array(
"Sarah" => 1,
"Sam" => 12,
"Tim" => 2,
"Tom" => 13,
 };

So when I being to type in S, the output would give me the numbers 1 and 12. Can anyone guide me towards the correct way of doing this? The current code I have is from here http://www.w3schools.com/php/php_ajax_php.asp.

Answers


Based on your reference link I have written this answer, use same HTML and JavaScript just replace the PHP with

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

<p><b>Start typing 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>

<?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 = array_search($name, $a);
            } else {
                $hint .= ", " . array_search($name, $a);
            }
        }
    }
}

// Output "no suggestion" if no hint was found or output correct values 
echo $hint === "" ? "no suggestion" : $hint;
?>

output

and for your case.

<?php
// Array with names
$a = array(
"Sarah" => 1,
"Sam" => 12,
"Tim" => 2,
"Tom" => 13,
 );
// 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 $key=>$name) {
        if (stristr($q, substr($key, 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;
?>

output


First of all, your array declaration is incorrect:

$a = array(
    'Sarah' => 1, //to make array associative you need to put key => value
    'Sam' => 12,
    'Tim' => 2,
    'Tom' => 13
);

And it returns both numbers because both start with 'S'. If you want to evaluate complete name you need to do this:

$numbers = [];

foreach($a as $key => $value) {
    if($key == $queryName)
        $numbers[] = $value;
}

echo $numbers;

This should return an array with numbers which coincide exactly with your request.


Need Your Help

How to tell how many frames a MovieClip spans on timeline

actionscript-3 flash

We have an effect we like to use where we synchronize a series of slides with a sound. As the sound plays, we show each slide (which is its own frame in a MovieClip), and slowly scale the slide up to

swift subviews not updating after unwind segue

ios iphone swift cocoa-touch uiviewcontroller

I am writing a basic iOS calendar app using swift. Currently, my main view contains one subview -- "month view" -- which contains five "week views" each of which contain 7 "day views." The main view