duanche2007 2013-01-17 01:38
浏览 60
已采纳

我的ajax脚本在一个div中加载我的整个index.php站点

I have created a simple text input box that will be used to fetch data from a databse using ajax.

<form method="post" action="">
        <input type="text" id="txt1" onkeyup="showHint(this.value);" />
</form>

The textbox calls an ajax function called showHint(str) in my ajax.js file as follows:

function showHint(str){

var xmlhttp;
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.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","index.php?page=getList&q="+str,true);
xmlhttp.send();

}

As you can tell from the above script it opens my PHP script that is used to fetch details from the databse like so:

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

$sql = "SELECT Name FROM Country WHERE Name LIKE '" . $q . "%'";

$recordSet = $_dbConn->query($sql);
$a = $recordSet->getrow();

 //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[$i],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;

The issue is that I am using a single Index.php page and so this line: "index.php?page=getList&q="+ is kinda like saying www.mydomain.com/index.php?index.php?page=getLet$q.

how can i fix this? I have tried to use POST and still not working. i have also tried to erase the index.php part in JS and that wont work either... thank you

  • 写回答

1条回答 默认 最新

  • doucong8553 2013-01-17 02:18
    关注

    as per @lethal-guitar - xmlhttp.open("GET","/index.php?page=getList&q="+str,true);

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿