我正在构建php框架,我尝试在搜索表单中实现一个ajax autosuggest函数,该函数将显示在每个页面上,但它仅适用于没有发送其他参数的页面。它工作正常但是当我点击搜索结果时,会出现错误,例如:
syntax error [Break On This Error]
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<script type="text/javascript" src="../js/autorsuggest.js"></script>
<!--search form-->
<form method="post" class="lsearch" action="<?php echo ROOT_URL. "/blogging/search"; ?>">
<label for="searchWord">Iskanje </label><br />
<input type="text" name="keywords" id="searchWord" maxlength="30" value="" class="inputText" onkeyup="autosuggest();return false;" />
<input type="submit" name="submit" id="submitSearch" value="Find" class="inputSubmit" />
<div id="results" ></div>
</form><!-- end form -->
.js文件:
function createObject() {
var request_type;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
request_type = new ActiveXObject("Microsoft.XMLHTTP");
} else {
request_type = new XMLHttpRequest();
}
return request_type;
}
var http = createObject();
function autosuggest() {
q = document.getElementById('searchWord').value;
// Set te random number to add to URL request
nocache = Math.random();
http.open('get', '../public/js/getarticlewords.php?q='+q+'&nocache = '+nocache);
http.onreadystatechange = autosuggestReply;
http.send(null);
}
function autosuggestReply() {
if(http.readyState == 4){
var response = http.responseText;
e = document.getElementById('results');
if(response!=""){
e.innerHTML=response;
e.style.display="block";
} else {
e.style.display="none";
}
}
}
和.php文件 //搜索单词 $搜索= $ _ GET [ “Q”];
$searching = Article::search_all($search);
echo "<ul>";
foreach($searching as $article){
if(strlen($search)>0){
echo "<li>";
do_url("/blogging/comment/$article->id", $article->title, "");
echo "</li>";
}
}
echo "</ul>";
我在其他带有ajax调用的页面上发现了同样的错误,但我解决了这个问题 改变aboslute的相对路径(js文件的路径),我不知道如何解决这个问题。 有任何想法吗?