[英]Php AJAX search with external JSON file
So Im trying to create an input field that displays results without the need of a refresh from an external JSON file.所以我试图创建一个显示结果的输入字段,而无需从外部 JSON 文件刷新。 My current code works fine, however how would I achieve checking for results in an external JSON file instead of directly in the php file?
我当前的代码工作正常,但是如何在外部 JSON 文件而不是直接在 php 文件中检查结果?
My JSON file: (I'd like to display results from "name").我的 JSON 文件:(我想显示来自“名称”的结果)。
[
{"name" : "300", "year" : "1999", "plot" : "X", "run" : "200 min", "rated" : "PG-13", "score" : "10/10", "source" : "A", "id" : "000"},
{"name" : "200", "year" : "1999", "plot" : "X", "run" : "200 min", "rated" : "PG-13", "score" : "10/10", "source" : "A", "id" : "000"}
]
My html and Javascript:我的 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>
gethint.php 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;
?>
You simply need to get the contents of your JSON file using file_get_contents
, convert it to an actual array using json_decode
, and get the name of each item to check them against your search query:您只需要使用
file_get_contents
获取 JSON 文件的内容,使用json_decode
将其转换为实际数组,并获取每个项目的名称以根据您的搜索查询进行检查:
<?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;
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.