내가

2011-05-06 4 views
-1
<script type="text/javascript"> 
    function showHint(str) { 
     if (str.length == 0) { 
      document.getElementById("txtHint").innerHTML = ""; 
      return; 
     } 
     if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari 
      xmlhttp = new XMLHttpRequest(); 
     } else { // code for IE6, IE5 
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
     } 
     xmlhttp.onreadystatechange = function() { 
      if (xmlhttp == 4 && xmlhttp.status == 200) { 
       document.getElementById("txtHint").innerHTML = xmlhttp.responseText; 
      } 
     } 
     xmlhttp.open("GET", "gethint.php?q=" + str, true); 
     xmlhttp.send(); 
    } 
</script> 


<form> 
    First name: <input type="text" onkeyup="showHint(this.value)" size="20" /> 
</form> 
<span id="txtHint"></span> 
------------------------------------- 


<?php 

$a[]="Anna"; 
$a[]="Wenche"; 
$a[]="Vicky"; 

//get the q parameter from URL 
$q=$_GET["q"]; 

//lookup all hints from array if length of q>0 
if (strlen($q) > 0) 
    { 
    $hint=""; 
    for($i=0; $i<count($a); $i++) 
    { 
    if (strtolower($q)==strtolower(substr($a[$a],0,strlen($q)))) 
     { 
     if ($hint=="") 
     { 
     $hint=$a[$i]; 
     } 
     else 
     { 
     $hint=$hint." , ".$a[$i]; 
     } 
     } 
    } 
    } 

// Set output to "no suggestion" if no hint were found 
// or to the correct values 
if ($hint == "") 
    { 
    $response="no suggestion"; 
    } 
else 
    { 
    $response=$hint; 
    } 

//output the response 
echo $response; 
?> 
+0

을하고있다? Fiddler 또는 Firebug를 보면 Ajax 호출이 서버로 연결되는 것을 볼 수 있습니까? 콘솔에 오류가 표시됩니까? – epascarello

+0

서버 또는 클라이언트에서 오류가 있습니까? 브라우저에서 Ajax 호출을 직접하고 있다는 URL을 호출하는 것에 지쳤습니까? 그게 효과가 있니? 그것이 속보하고있는 지점을 찾아야합니다. – epascarello

+0

파이어 버그의 호출 및 응답을 확인하십시오. 코드는 정상입니다. – booota

답변

3

내가 오류가 발생할 것이라고 생각

if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q)))) 
+0

여기 우승자가 있다고 생각하십시오! :) – epascarello

-2
<form> 
    FirstN<input type="text" onkeyup="return showHint(this.value);" size="20" /> 
</form> 
+0

ok ... 감사합니다 ... – Partyboy

+0

왜 그렇게해야합니까? 좋은 프로그래머라면 눈에 띄지 않게 추가 할 것이고, 이벤트 객체 자체를 통해 할 수있는 이벤트를 취소해야 할 필요가 없다면 return 문이 필요 없다. – epascarello

+0

이 답변으로 마술처럼 포스터의 코드 작업이 이루어지지는 않을 것입니다. – epascarello

0
if (xmlhttp == 4 && xmlhttp.status == 200) { 
Should BE 
if (xmlhttp.readyState==4 && xmlhttp.status==200) 

--------------------------- 

if (strtolower($q)==strtolower(substr($a[$a],0,strlen($q)))) 
Should BE 
if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q)))) 
0

이 줄

if (strtolower($q)==strtolower(substr($a[$a],0,strlen($q)))) 

을 변경해보십시오 텍스트 상자에 입력 할 때 내 아약스 전화가 작동하지 않습니다

strtolower(substr($a[$a],0,strlen($q)))) 

여기 $ a [$ a] .. 란 무엇입니까?

for($i=0; $i<count($a); $i++) 
    { 
    if (strtolower($q)==strtolower(substr($a[$a],0,strlen($q)))) 

는 $로 교체하십시오이

for($i=0; $i<count($a); $i++) 
    { 
    if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q)))) 

$ 같이 변경하십시오 그런 다음 루프 실행에처럼 난 후 단지 $ A [] 무엇을 작동하지