使用AJAX从PHP数组中获取数据

时间:2016-03-14 22:55:31

标签: php arrays ajax

我试图使用AJAX来帮助我从PHP数组中检索数据。我希望能够输入一个名字,然后输出相应的号码。我已经玩过W3schools的AJAX PHP代码了,但是不知道怎么改变它给我相应的号码我想找?我的数组看起来像这样:

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

因此,当我输入S时,输出会给出数字1和12。 任何人都可以指导我做正确的方法吗?我现有的代码来自http://www.w3schools.com/php/php_ajax_php.asp

2 个答案:

答案 0 :(得分:0)

首先,您的数组声明不正确:

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

它返回两个数字,因为它们都以' S'开头。如果您想评估完整名称,您需要这样做:

$numbers = [];

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

echo $numbers;

这应该返回一个数字,该数组与您的请求完全一致。

答案 1 :(得分:0)

根据您的参考链接,我写了这个答案,使用相同的HTML和JavaScript只需用PHP替换

<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;
?>

输出

enter image description here

和你的情况。

<?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;
?>

输出

enter image description here