Search pe site cu Ajax
Scris: Joi Sep 29, 2011
Salut! Vreau sa ofer posibilitatea unui search pe site. Imi puteti spune unde am gresit??Ms anticipat.
Acesta este codul meu:
fisierul index.html
fisierul search.js
fisierul searchWord.php
Acesta este codul meu:
fisierul index.html
Cod: Selectaţi tot
<html>
<head>
<script type="text/javascript" src="search.js"></script>
</head>
<body >
<form name="searchNameForm">
<input type="text" maxlength="60" name="wordToSearchFor" id="edit-name" style="width: 35%" onKeyUp="searchProduct(this.value)" />
</form>
<div id="res" > </div>
</body>
</html>
Cod: Selectaţi tot
function GetXmlHttpObject()
{
var objXMLHttp=null
if (window.XMLHttpRequest)
{
objXMLHttp=new XMLHttpRequest()
}
else if (window.ActiveXObject)
{
objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
}
return objXMLHttp;
}
var req;
function searchProduct(word)
{
req=GetXmlHttpObject();
if (req == "")
{
alert ("Your browser does not support AJAX!");
return;
}
var url="searchWord.php";
url=url+"?wordToSearchFor"+word;
req.onreadystatechange=function()
{
if (req.readyState == 4)
if (req.status == 200)
if (req.responseText == "")
{
document.getElementById("res").innerHTML="<span style="color:red">The recipe doesn't exist!</span>";
}
else
{
document.getElementById("res").innerHTML=req.responseText;
}
}
req.open("GET",url,true);
req.send(null);
}
Cod: Selectaţi tot
include "dbconnect.php";
if (isset($_GET['word']))
$word=trim($_GET['word']);
$data="";
if (!empty($word))
{
$word="%".$word."%";
$result=mysql_query("select name from products where name like '$word'");
$nr=mysql_num_rows($result);
if ($nr)
{
$data.="<ul>";
while ($row=mysql_fetch_row($result))
{
$data.="<li><i><b>".$row[0]."</b></i>:".$row[1]."</li>";
}
$data.="</ul>";
}
}
echo $data;