2016-03-14 79 views
0

我想使用AJAX来帮助我从我的PHP数组中检索数据。我希望能够输入一个名字,并拿出相应的号码。我玩过W3school的AJAX PHP代码,但不知道如何改变它以给我相应的数字,我正在寻找?我的阵列看起来像这样:使用AJAX从PHP数组中获取数据

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

所以,当我正在键入S,输出会给我的号码1和12 任何人都可以指导我对这样做的正确方法是什么?我现在的代码是从这里http://www.w3schools.com/php/php_ajax_php.asp

回答

0

基于我写了这个答案大家参考链接,使用相同的HTML和JavaScript只是

<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。

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

+1

谢谢!这正是我所期待的。 – snazzyjazzy

-1

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

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

这应该与你的要求相吻合正是数字返回数组。

+0

如何号码分开吗? –