【问题标题】:Using AJAX to get data from a PHP array使用 AJAX 从 PHP 数组中获取数据
【发布时间】:2016-06-30 04:56:12
【问题描述】:

我正在尝试使用 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

【问题讨论】:

    标签: php arrays ajax


    【解决方案1】:

    根据您的参考链接,我已经写了这个答案,使用相同的 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;
    ?>
    

    输出

    对于你的情况。

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

    输出

    【讨论】:

    • 谢谢!这正是我想要的。
    【解决方案2】:

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

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

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

    【讨论】:

    • 数字是如何分开的?
    猜你喜欢
    • 2019-11-26
    • 1970-01-01
    • 1970-01-01
    • 2023-01-16
    • 1970-01-01
    • 1970-01-01
    • 2016-05-14
    • 2011-09-17
    • 1970-01-01
    相关资源
    最近更新 更多