duanquyong8164 2013-04-26 03:02
浏览 33
已采纳

php在ajax中返回undefined [关闭]

I have tried to make search suggestions like google...

i created a table tags and a single column tag and it has some tags stored in it but the problem is when i type nothing or something that is in the DB i get undefined as return

but when i type something that is not in DB i get sorry [which is correct]

i am using Ajax

my php code

<?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
  • 写回答

2条回答 默认 最新

  • douyongdao4046 2013-04-26 03:26
    关注
       $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']; 
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改