A lightweight script which I wrote today (Works with modern and older browsers IE 6+). You can create a new .JS, .PHP, and .CSS file to separate those, but I was lazy to do so, so here goes all together.
To include more data, you can change the line: [ICODE]$response .= '<div class="results"><a href="/torrents-details.php?id=' . $i['id'] . '">' . $i['name'] . '</a></div>';[/ICODE]
The hard limit is that search query must have at least 4 characters (to avoid db stressing)
To do a quick test, create a new test.php file
Preview:
To include more data, you can change the line: [ICODE]$response .= '<div class="results"><a href="/torrents-details.php?id=' . $i['id'] . '">' . $i['name'] . '</a></div>';[/ICODE]
The hard limit is that search query must have at least 4 characters (to avoid db stressing)
To do a quick test, create a new test.php file
<?php
include 'backend/functions.php';
dbconn();
// This is the PHP side ajax check
if (isset($_GET['ajax']) && $_GET['ajax'] === '1')
{
if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
$searchstr = strtolower(trim($_POST["query"]));
$search = "MATCH (torrents.name) AGAINST ('".mysqli_real_escape_string($GLOBALS["DBconnector"],$searchstr)."' IN BOOLEAN MODE)";
if (!empty($_POST["query"]) && strlen($_POST["query"]) >= 4)
{
// You can JOIN tables here to get more information about torrents. (But will affect performance)
$stmt = SQL_Query_exec("SELECT * FROM torrents WHERE $search LIMIT 10");
$row = mysqli_fetch_assoc($stmt);
}
}
echo '<div class="ls_box">
<div style="text-align: center;border-bottom: 1px solid #eee;padding-bottom: 5px;">Found results for <span style="color: #e6750c; ">' . $_POST['query'] . '</span></div>';
if ($row)
{
$response = '';
foreach ($stmt as $i)
{
$response .= '<div class="results"><a href="/torrents-details.php?id=' . $i['id'] . '">' . $i['name'] . '</a></div>';
}
}
else
{
$response = '<div class="results">No results found</div>';
}
echo $response;
echo '</div>';
die;
}
stdhead('ajax search');
begin_frame('ajax search');
?>
Search torrents: <input type="text" onkeyup="ajaxSearch(this.value)" name="query" />
<div id="ajaxSearchStatus"></div>
<style>
.ls_box {
position: absolute;
background: #fff;
width: 20%;
border-bottom: 0;
border-radius: 10px;
padding: 10px;
box-shadow: 0 1px 4px rgba(0,0,0,.1);
font-weight: bold;
}
.results {
border-bottom: 1px solid #eee;
padding: 5px;
word-wrap: break-word;
}
.results a {
color: #111;
text-decoration: none;
}
.results a:hover {
color: #333;
}
.results:hover {
background: #e6750c3b;
border-right: 2px solid #e6750c;
}
</style>
<script type="text/javascript">
function ajaxSearch(str)
{
var xhttp;
if (str.length < 4) {
document.getElementById("ajaxSearchStatus").innerHTML= '';
return false;
}
// Modern browser support
if (window.XMLHttpRequest)
{
xhttp = new XMLHttpRequest();
}
// Old browser support (IE 6+)
else
{
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
var url = '?ajax=1';
var params = 'query=' + str;
xhttp.open("POST", url, true);
// Send the proper header information along with the request
xhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
// What to do with the response
xhttp.onreadystatechange = function()
{
if (this.readyState === 4 && this.status === 200)
{
document.getElementById("ajaxSearchStatus").innerHTML = this.responseText;
}
};
// Send the post fields
xhttp.send(params);
// Until data returned, show custom text
document.getElementById("ajaxSearchStatus").innerHTML = '<div class="ls_box">Loading data...</div>';
}
</script>
<?php
end_frame();
stdfoot();
Preview:
