Bu yüzden harici bir JSON dosyasından yenileme gerektirmeden sonuçları görüntüleyen bir giriş alanı oluşturmaya çalışıyorum. Geçerli kodum iyi çalışıyor, ancak doğrudan php dosyasında değil, harici bir JSON dosyasındaki sonuçları nasıl kontrol edebilirim?Php Harici JSON dosyası ile AJAX araması
JSON dosyam: (Sonuçları "ad" dan göstermek istiyorum).
[
{"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"}
]
Benim html ve 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 Sadece, file_get_contents
kullanarak JSON dosyasının içeriğini almak kullanarak gerçek bir diziye dönüştürmek gerekir
<?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;
?>
'$ films = json_decode (dosya_get_contents (" your_json_file.json ")) – Chris
$ film değişkenini php koduna nereye koyardım? –
Bunu, $ a' değişkeninizin yerine kullanırsınız, ancak her bir filmin '' a '' öğesinin her birini kullanarak doğrudan yerine '' filmin '' anahtarına erişin. – Chris