显示在搜索结果中的记录与阿贾克斯

问题描述:

我有以下ajax.js,我必须使用:显示在搜索结果中的记录与阿贾克斯

var xmlRequest = null; 
    function ajax(url, parametersArray, callbackFunction, fcnVars) { 
     if (xmlRequest != null) { 
     if (xmlRequest.readyState == 2 || xmlRequest.readyState == 3) { 
      xmlRequest.abort(); 
      xmlRequest = null; 
      } 
     } 
     if (parametersArray == null) 
      parameters = ""; 
     else 
      parameters = formatParameters(parametersArray); 
     if (window.XMLHttpRequest) 
      xmlRequest = new XMLHttpRequest(); 
     else 
      xmlRequest = new ActiveXObject("MSXML2.XMLHTTP.3.0"); 
      xmlRequest.open("POST", url, true); 
      xmlRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
      xmlRequest.onreadystatechange = function() { 
       if (xmlRequest.readyState == 4 && xmlRequest.status == 200) { 
        if (xmlRequest.responseText) { 
         callbackFunction(xmlRequest.responseText, fcnVars); 
        } 
       } 
      } 
     xmlRequest.setRequestHeader("Content-length", parameters.length); 
     xmlRequest.setRequestHeader("Connection", "close"); 
     xmlRequest.send(parameters); 
    } 

    function formatParameters(parameters) { 
     var i = 0; 
     var param = ""; 
     for (index in parameters) { 
      if (i==0) { 
       param += index+"="+urlencode(parameters[index]); 
      } else { 
       param += "&"+index+"="+urlencode(parameters[index]); 
      } 
      i++; 
     } 
    return param; 
    } 

    function urlencode(clearString) { 
     clearString = encodeURI(clearString); 
     clearString = clearString.replace('&', '%26'); 
     return clearString; 
    } 

和我有以下MySQL表:

CREATE TABLE `dictionary` (
    `word` varchar(64) NOT NULL, 
    PRIMARY KEY (`word`) 
) ENGINE=MyISAM DEFAULT CHARSET=latin1 

就完了,在这里是我的搜索页面:

<div id = "search"> 
    <form id="searchform" method="post"> 
    Search for Word: 
    </select> 
    <input type="text" id="search_term" name="search_term" /> 
    <input type="submit" id="cmdSearch" value="Search" /> 
</form> 
<div id="search_results"></div> 
</div>  

现在,我必须创建一个PHP函数,它将返回一个数组与表中找到的单词,使用以上ajax.js

应使用ajax在search_results div中显示结果。

当然,我也需要一个javascript代码。

任何人都可以帮助我开始构建?我用jQuery做了类似的事情,但是现在我必须使用这个脚本,而且我没有其他办法可以做到这一点。

目标是在不刷新的情况下在php页面中显示结果。

任何帮助将十分赞赏

更新:

这里是我的PHP代码:

<?php 

// add encoding here 
header("Content-Type: text/html; charset=iso-8859-7"); 

// include the database connection here 
include 'config.php'; 
include 'openDb.php'; 
function findWords(){ 
    // sanitaze the user input 
    $term = strip_tags(substr($_POST['search_term'],0, 100)); 
    $term = mysql_escape_string($term); 

    // query the database. one fileld only, so nothing to optimize here 
    $sql = "SELECT word FROM dictionary WHERE word like '%$term%'"; 
    $result = mysql_query($sql); 

    // set the string variable 
    $string = ''; 

    // if resulta are found then populate the string variable 
    if (mysql_num_rows($result) > 0){ 
     while($row = mysql_fetch_object($result)){ 
      // display the results here in bold and add a new line or break after each  result 
      $string[] = "<b>".$row->user_name."</b><br/>\n"; 
     } 
    } else { 
     // if no results are found, inform the visitors... 
     $string[] = "No matches!"; 
    } 
    // output the string 
    return $string[]; 

这里是JavaScript:

<script type='text/javascript'> 
    ajax("findWord.php", {id:search_term}, function(result,params) { 
    alert("result for ID: "+params.id+"\n\n"+result); 
    }, {id:search_term}); 
    </script> 
+1

您是否尝试过自己建立呢?你有什么困难? – Armatus 2012-04-19 18:59:03

+0

好吧,我已经完成了PHP,除了我不确定它是否会起作用。我最大的问题是javascript调用 – Zoran 2012-04-19 19:05:12

+0

那么你有什么试图建立所需的JavaScript? – Armatus 2012-04-19 19:07:19

你将不得不依赖在ajax功能,它可以让你访问任何它加载在呼叫ACK功能:

callbackFunction(xmlRequest.responseText, fcnVars); 

而且ajax解释了它应该如何称呼自己:

ajax(url, parametersArray, callbackFunction, fcnVars) 

即使parametersArray实际上应该是一个对象({index:value, i1:v2,...}),而不是一个数组([val1, val2,...])。 fcnVars可以是包含任何要传递给回调函数的对象。

这应该工作:

ajax("add_page.php", {id:535}, function(result,params) { 
    alert("result for ID: "+params.id+"\n\n"+result); 
}, {id:535}); 
+0

开始让我测试它,我会在这里发送反馈... – Zoran 2012-04-19 19:25:52

+0

我没有身份证..我应该使用search_term,而不是我猜? – Zoran 2012-04-19 19:32:49

+0

尝试,如果事情不行,研究为什么;如果在广泛的查找和尝试之后找不到任何东西,请询问:) – Armatus 2012-04-19 19:34:59