I have built an ajax post and I am sending some data to another php file through it, finally doing a live search... but the thing is that it's a bit slow, when I type something inside the box, the ajax takes between 1-2 seconds to fire....I want the search to be like youtube's search or facebook search, very quick, when I type something there it immediately shows suggestions... any ideas how can I fix this ?
Here is the code:
$("#search").keyup(function(){
var value = $(this).val();
var categ = $("#categ").val();
if (value.length >= 1)
{
$.ajax({
type: "POST",
url: "../core/search.php",
data: { string : value , categ : categ },
success: function(result){
$(".each_movie").hide();
$(".search_movie").remove();
$("#movies").append(result);
}
});
}
else
{
$(".each_movie").show();
$(".search_movie").remove();
}
});
and PHP
$src = $_POST['string'];
$categ = $_POST['categ'];
$sql = mysql_query("SELECT * FROM movies WHERE `$categ` LIKE '%".$src."%' ") or die(mysql_error());
while ($sql_grab = mysql_fetch_assoc($sql))
{
?>
<div class="search_movie">
<p style="margin-bottom:5px;font-family:'Lucida Sans Unicode', 'Lucida Grande', sans-serif;color:rgba(255,255,255,0.3);font-size:12px;">Din: <?php echo $sql_grab['airdate']; ?> in cinematografe.</p>
<table>
<tr>
<td rowspan="2" valign="top"><img class="main_pic" src="<?php echo $sql_grab['mainpic']; ?>" width="180px" /></td>
<td><p class="name"><?php echo $sql_grab['name']; ?></p></td>
</tr>
<tr>
<td><div class="desc_wrapper" id="<?php echo $id; ?>" style="display:inline-block;"><p class="desc"><?php echo mysql_real_escape_string($sql_grab['description']); ?></p></div><p nr="<?php echo $id; ?>" id="more<?php echo $id; ?>" class="more" style="position:absolute;margin:30px 2px;color:rgba(255,255,255,0.2);cursor:pointer;display:inline-block">...citeşte</p></td>
</tr>
</table>
</div><!--each movie end-->
<?php
}
Use JSON to speed up your Ajax script’s connection between it and the server.
You can use JSONP to increase speed of ajax,You will get more details from http://www.jquery4u.com/json/jsonp-examples/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With