PHP在ajax中返回undefined


php returning undefined in ajax

我尝试过像google这样的搜索建议…

我创建了一个表标签和一个单列标签,它有一些标签存储在其中但问题是,当我输入什么都没有或东西是在DB我得到未定义的返回

但当我输入的东西不是在DB我得到抱歉[这是正确的]

我正在使用Ajax

我的PHP代码

<?php
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';
echo '<response>';
        $searchvalue=$_GET['searchvalue'];
        $con=mysqli_connect("localhost","root","","myweb") or die("error connecting db");
        $sqlresult = mysqli_query($con,"SELECT tag FROM tags where tag like '".$searchvalue."%'");
        $suggests="";
        while($row = mysqli_fetch_array($sqlresult))
        {
            $suggests=$suggests.$row.',';
        }
        //$suggarray=explode(",",$suggests);
        if(strlen($suggests)>0)
        {
            echo "found";
        }
        else
        {
            echo "sorry! for ".$searchvalue;
        }
        mysqli_close($con);
    echo '</response>';
?>
JavaScript for ajax

// JavaScript for search ajax
var xmlHttp= createXmlHttpRequestObject();
function createXmlHttpRequestObject(){
    //alert("create obj");
   var xmlHttp;
   if(window.ActiveXObject){
      try{
         xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
         }catch(e){
            xmlHttp =false;
            }
      }else{
         try{
            xmlHttp= new XMLHttpRequest();
            }catch(e){
               xmlHttp =false;
               }
         }
      if(!xmlHttp)
            alert("cant create that object hoss!");
      else
            return xmlHttp;
   }
function searchprocess(){ 
    //alert("start");
    if(xmlHttp.readyState==0 || xmlHttp.readyState==4){
      searchtext=encodeURIComponent(document.getElementById("searchvalue").value );
      xmlHttp.open("GET", "pages/search_suggestions.php?searchvalue="+searchtext, true);
      xmlHttp.onreadystatechange = handleServerResponse;
      xmlHttp.send(null);
      }else{
         setTimeout('searchprocess()', 1000);
         }
   }

function handleServerResponse(){
    //alert("handle");
   if(xmlHttp.readyState==4){
            //alert("handle ready");
            if(xmlHttp.status==200 || xmlHttp.status==304){
            //alert("inside");
               xmlResponse=xmlHttp.responseXML;
               xmlDocumentElement=xmlResponse.documentElement;
              message=xmlDocumentElement.firstChild.data;
               document.getElementById("tempo").innerHTML=message;
               setTimeout('searchprocess()', 1000);
         }else{
            alert("Something went wrong!");
            }
      }
   }

// end of JavaScript for search ajax

由于php脚本是由AJAX调用的,所以不是单独的回显,而是将它们连接成一个字符串,并在末尾回显一次:

header('Content-Type: text/xml');
$resp = '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';
$resp .= '<response>';
..and so on... until
$resp .= '</response>';

then echo $response;

   $sqlresult = mysqli_query($con,"SELECT tag FROM tags where tag like '".$searchvalue."%'");
    $suggests="";
    while($row = mysqli_fetch_array($sqlresult))
    {
$comma=$suggests?',':'';
        $suggests.=$comma.$row['tag']; 
    }