Javascript 使用Ajax显示搜索结果

Javascript 使用Ajax显示搜索结果,javascript,php,html,ajax,Javascript,Php,Html,Ajax,我试图使用Ajax来显示搜索结果,但它不起作用,我无法找到原因。这是我的.html文件,其中也包含Ajax JS function showSearchResult() { alert('beginning'); if (document.getElementById("search").value == "") { document.getElementById("searchResult").innerHTML = ""; return;

我试图使用Ajax来显示搜索结果,但它不起作用,我无法找到原因。这是我的.html文件,其中也包含Ajax

JS

function showSearchResult() {
    alert('beginning');
    if (document.getElementById("search").value == "") {
        document.getElementById("searchResult").innerHTML = "";
        return;
    }
    try {
        xhr = new XMLHttpRequest();
    } catch (e) {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
    // handle old browsers
    if (xhr == null) {
        alert("Ajax not supported by your browser!");
        return;
    }
    var string = document.getElementById("search").value;       
    var url = "quote4.php?search=" + string;


    xhr.onreadystatechange = handler;
    xhr.open("GET", url, true);
    xhr.send(null);
}

function handler() {

    // only handle loaded requests
    if (xhr.readyState == 4) {
        if (xhr.status == 200) document.getElementById("searchResult").innerHTML = xhr.responseText;

        else alert("Error with Ajax call!");
    }

}
HTML

<h1>Live Search</h1>

<form>
    <input id="search" size="100" type="search"> <input id="submit"
    onclick="showSearchResult(); return false;" type="button" value="Go">
</form>

<div id="searchResult"></div>       
实时搜索
PHP

<?
// pretend server is slow


// try to get quote
$handle = @fopen("http://download.finance.yahoo.com/d/quotes.csv?s={$_GET['search']}&f=e1l1hg", "r");
if ($handle !== FALSE)
{
    $data = fgetcsv($handle);
    if ($data !== FALSE && $data[0] == "N/A")
    {
        print("Price: {$data[1]}");
        print("<br />");
        print("High: {$data[2]}");
        print("<br />");
        print("Low: {$data[3]}");
    }
    fclose($handle);
}
?>

处理程序函数末尾缺少一个
}
。您在提交的
上也拼写错了函数调用
showSreachResult()
,应该是
showSearchReuslt())
-PS-还有拼写错误Lol关于拼写错误,你是对的,但我检查了是否有遗漏}但事实并非如此。在修复拼写错误后,仍然无法在帖子中编辑代码,无论你做了什么更改