Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery ajax is slow

Tags:

ajax

php

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   
}
like image 256
southpaw93 Avatar asked Apr 10 '13 09:04

southpaw93


2 Answers

Use JSON to speed up your Ajax script’s connection between it and the server.

like image 126
roshan lal Avatar answered Sep 21 '22 10:09

roshan lal


You can use JSONP to increase speed of ajax,You will get more details from http://www.jquery4u.com/json/jsonp-examples/

like image 20
Shijin TR Avatar answered Sep 21 '22 10:09

Shijin TR