Sam Granger
12-21-2005, 06:57 PM
search.php
<?php
if (isset($_GET['q']) === FALSE) {
exit();
}
$root = '../';
include($root . 'connect.php');
$sQuery =
"SELECT *
FROM news
WHERE NewsContent LIKE '%". mysql_real_escape_string(trim($_GET['q'])) ."%'";
$rResult = mysql_query($sQuery);
if ($rResult === FALSE) {
print 'Kon de query '. $sQuery .' niet uitvoeren.';
}
$aTmp = array();
if (mysql_num_rows($rResult) == 0) {
print 'No Results';
}
else {
while ($aTmp = mysql_fetch_assoc($rResult)) {
echo "<h1>{$aTmp['NewsTitle']}</h1>";
echo substr($aTmp['NewsContent'],0,300) . '...';
echo "<a href=\"{$aTmp['Url']}\" title=\"{$aTmp['NewsTitle']}\">Read More</a><br />";
}
}
?>
search.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Live zoeken in je database.</title>
<script language="Javascript" type="text/javascript">
// Het script die daadwerkelijk het zoeken doet
var url = "./scripts/search.php?q=";
function handleHttpResponse() {
// Hier wordt de output van het script gehaald.
if (http.readyState == 4 && http.status == 200) {
document.getElementById('results').innerHTML = http.responseText;
}
}
var done = false;
function liveSearch() {
if (!done && http) {
var term = document.getElementById('searchterm').value;
document.getElementById('results').innerHTML = "";
http.open('GET', url + term, true);
http.onreadystatechange = handleHttpResponse;
done = true;
http.send(null);
}
}
function getHTTPObject() {
var xmlhttp;
/*@cc_on
@if (@_jscript_version >= 5)
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
@else
xmlhttp = false;
@end @*/
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp = false;
}
}
return xmlhttp;
}
var http = getHTTPObject();
</script>
</head>
<body>
<form action="post">
<p>Zoekterm:</p>
<input type="text" id="searchterm" value="">
<input type="button" id="search" value="Zoek!" onclick="liveSearch();">
</form>
<div id="results"> </div>
</body>
</html>
The above script searches a database and it displays results. It all works. However, once I see the results, I can't search again without refreshing the search.html - even though I still can see the search box etc...
How can I make it so that when I would click on search again it clears all the current results and displays the newer results?
Thanks in advance,
Sam
<?php
if (isset($_GET['q']) === FALSE) {
exit();
}
$root = '../';
include($root . 'connect.php');
$sQuery =
"SELECT *
FROM news
WHERE NewsContent LIKE '%". mysql_real_escape_string(trim($_GET['q'])) ."%'";
$rResult = mysql_query($sQuery);
if ($rResult === FALSE) {
print 'Kon de query '. $sQuery .' niet uitvoeren.';
}
$aTmp = array();
if (mysql_num_rows($rResult) == 0) {
print 'No Results';
}
else {
while ($aTmp = mysql_fetch_assoc($rResult)) {
echo "<h1>{$aTmp['NewsTitle']}</h1>";
echo substr($aTmp['NewsContent'],0,300) . '...';
echo "<a href=\"{$aTmp['Url']}\" title=\"{$aTmp['NewsTitle']}\">Read More</a><br />";
}
}
?>
search.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Live zoeken in je database.</title>
<script language="Javascript" type="text/javascript">
// Het script die daadwerkelijk het zoeken doet
var url = "./scripts/search.php?q=";
function handleHttpResponse() {
// Hier wordt de output van het script gehaald.
if (http.readyState == 4 && http.status == 200) {
document.getElementById('results').innerHTML = http.responseText;
}
}
var done = false;
function liveSearch() {
if (!done && http) {
var term = document.getElementById('searchterm').value;
document.getElementById('results').innerHTML = "";
http.open('GET', url + term, true);
http.onreadystatechange = handleHttpResponse;
done = true;
http.send(null);
}
}
function getHTTPObject() {
var xmlhttp;
/*@cc_on
@if (@_jscript_version >= 5)
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
@else
xmlhttp = false;
@end @*/
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp = false;
}
}
return xmlhttp;
}
var http = getHTTPObject();
</script>
</head>
<body>
<form action="post">
<p>Zoekterm:</p>
<input type="text" id="searchterm" value="">
<input type="button" id="search" value="Zoek!" onclick="liveSearch();">
</form>
<div id="results"> </div>
</body>
</html>
The above script searches a database and it displays results. It all works. However, once I see the results, I can't search again without refreshing the search.html - even though I still can see the search box etc...
How can I make it so that when I would click on search again it clears all the current results and displays the newer results?
Thanks in advance,
Sam