Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/450.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用外部JSON文件进行Php AJAX搜索_Javascript_Php_Ajax - Fatal编程技术网

Javascript 使用外部JSON文件进行Php AJAX搜索

Javascript 使用外部JSON文件进行Php AJAX搜索,javascript,php,ajax,Javascript,Php,Ajax,因此,我试图创建一个输入字段来显示结果,而不需要从外部JSON文件刷新。我当前的代码运行良好,但是如何在外部JSON文件中而不是直接在php文件中检查结果 我的JSON文件:(我想显示“name”的结果) 我的html和Javascript: <script> function showHint(str) { if (str.length == 0) { document.getElementById("txtHint").innerHTML = ""; retur

因此,我试图创建一个输入字段来显示结果,而不需要从外部JSON文件刷新。我当前的代码运行良好,但是如何在外部JSON文件中而不是直接在php文件中检查结果

我的JSON文件:(我想显示“name”的结果)

我的html和Javascript:

<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", "gethint.php?q=" + str, true);
    xmlhttp.send();
}
}
</script>
<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>

函数showHint(str){
如果(str.length==0){
document.getElementById(“txtHint”).innerHTML=“”;
返回;
}否则{
var xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
document.getElementById(“txtHint”).innerHTML=xmlhttp.responseText;
}
};
open(“GET”、“gethint.php?q=“+str,true”);
xmlhttp.send();
}
}
开始在下面的输入字段中键入名称:

名字: 建议:

gethint.php

<?php
//Instead of using these I'd like to use the external JSON file
$a[] = "Anna";
$a[] = "Brittany";
$a[] = "Cinderella";
$a[] = "Diana";
// 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;
?>

您只需使用获取JSON文件的内容,使用将其转换为实际数组,并获取每个项目的名称,以对照搜索查询进行检查:

<?php

// Here, the TRUE parameter is very important to treat it as an associative array
$films = json_decode(file_get_contents("path/to/your_json_file.json"), true);

// get the q parameter from URL if it is set
$q = isset($_REQUEST["q"]) ? $_REQUEST["q"] : "";
$hint = "";
// lookup all hints from array
$q = strtolower($q);
$len = strlen($q);
// For each film
foreach($films as $film) {//                                     <------
    // Get the name
    $name = $film['name'];//                                     <------
    // If the query is empty or if the query is found
    if ($q === "" || 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;


?>


$films=json\u decode(file\u get\u contents(“your\u json\u file.json”))
我将$films变量放在php代码中的什么位置?您将使用它代替
$a
变量,但是访问每部影片的
名称
键,而不是直接使用
$a
的每个元素。您知道如果没有输入搜索词,是否有办法使其显示所有建议。因为当前如果我搜索某个内容,然后删除搜索词,建议将保留。我想让它显示所有的建议。…@S.Doe当然,我编辑了我的答案来做这件事(如果
$q
为空,它将打印所有的名字)。
<?php

// Here, the TRUE parameter is very important to treat it as an associative array
$films = json_decode(file_get_contents("path/to/your_json_file.json"), true);

// get the q parameter from URL if it is set
$q = isset($_REQUEST["q"]) ? $_REQUEST["q"] : "";
$hint = "";
// lookup all hints from array
$q = strtolower($q);
$len = strlen($q);
// For each film
foreach($films as $film) {//                                     <------
    // Get the name
    $name = $film['name'];//                                     <------
    // If the query is empty or if the query is found
    if ($q === "" || 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;


?>