0

dudWhen i call to my php file using ajax, it takes almost a full 30 seconds before any results are displayed.

How could i make the results more instantaneous?

my ajax is:

$("#j_search").keyup(function(){

        if(($.trim($(this).val()) != '') & ($(this).val() != ' ')){

            $.post('../php/ajax/j_search.php', {search: $(this).val()}, function(data){
                $(".matched_results").html(data).show();
            });
        }
    });

If you for some reason think my php is wrong, then write a sample of your own and i will test that, though i doubt it. I can not give the php source code.

I could get fired for releasing code but it just seems worth it at times like these.. Here is the a snippet of the PHP:

class SQL_COMMANDS{
    //Connecting vars
    private $_HOST = "localhost";
    private $_USER = "root";
    private $_PASS = "dud";
    private function HANDLE($ERRORNUM, $ERRORMSG){//USED FOR ERROR HANDLER
        $HANDLE = new HANDLE();
        $HANDLE->DIRECT($ERRORNUM, $ERRORMSG);
    }

    function connect($DB){
        $con=mysqli_connect($this->_HOST,$this->_USER,$this->_PASS,$DB);
        if (mysqli_connect_errno($con)){
            $this->HANDLE("001","Connection failed (DB)");
        }
        return $con;
    //  mysqli_close($con);
        //$SQL_COMMANDS->connect("DB");
}

    function SQL($DB, $SQL){//Construct used to transact CONNECT
        return mysqli_query($this->connect($DB), $SQL);
    //  mysqli_close($this->connect($DB));
        //$SQL_COMMANDS->SQL("DB", "SELECT * FROM TABLE WHERE ID=1");
    }


    function Assoc_Grab($DB, $SQL, $ARRAYGRAB){//Returns Data associated with the ARRAY directed from the QUERY
        $DATA = array();
        $ROW = mysqli_fetch_assoc($this->SQL($DB, $SQL));
        foreach($ARRAYGRAB as $pointer){
            array_push($DATA,$ROW[$pointer]);
        }
        //print_r($SQL_COMMANDS->Assoc_Grab('DB', "SELECT * FROM TABLE WHERE ID='1'", array('NAME')));
        return $DATA;
    }
}
  • Have you tried having `../php/ajax/j_search.php` just be `` to see how fast it responds when just echoing your ajax posted data? – Sean Aug 13 '13 at 01:04
  • if you fetch some other url, for exemple a `test.txt`-file, is it still slow? – Puggan Se Aug 13 '13 at 01:04
  • if you type the word "test" into #j_search, you will be triggering the ajax 4 times, that may be the reason why its feals slow – Puggan Se Aug 13 '13 at 01:06
  • i have tested my php file with echo and it is instant. However, i am still not convinced anything is wrong with the mysqli in the php –  Aug 13 '13 at 01:06
  • i also thought that @puggan, but when i typed one character it still took about 30 seconds –  Aug 13 '13 at 01:07
  • Have you tried accessing `../php/ajax/j_search.php` directly, and how long does it take to load? – Sean Aug 13 '13 at 01:07
  • 1
    What is your php file pulling data from? Try echoing out timestamps throughout the php execution and see where it is slow. – thaspius Aug 13 '13 at 01:12
  • tested your JS, worked nice, see http://jsfiddle.net/Fjdnk/ – Puggan Se Aug 13 '13 at 01:14
  • 1
    The php you added to your edit doesn't include the section handling the POST response. – thaspius Aug 13 '13 at 01:17

2 Answers2

0

Where is your server hosted? Have you done baseline ping tests to check for latency issues?

Try creating a php page that echos out just a single number, letter, etc and then use google chrome developer tools to run the following in the console:

$.get('../php/ajax/j_search.php', function(data){
    console.log(data);
});

It would also be worthwhile to check the network tab for the results of the $.get after it finishes. Hover over the blue timeline bar. It will give you a breakdown of the times for connecting, waiting, sending and receiving.

thaspius
  • 1,135
  • 3
  • 17
  • 33
  • its hosted on my localhost, i dont think is what your suggesting, the same code loads instantly when submitted via form –  Aug 13 '13 at 01:11
0

There's nothing incorrect about the JavaScript code that you wrote. It's definitely a problem with either your PHP, or the server that you're using.

Try to replace your PHP (i.e.: php/ajax/j_search.php) with a simple code like this one:

<?php
echo rand();
?>

Now every time you press any key while the focus is on the "#j_search" textbox you should see almost instantaneously a new random number. If that still taking a while, then you might need to check your server. Try to restart your machine, that might solve the problem!

Edit1: Also check this answer: WAMP/XAMPP is responding very slow over localhost

Edit2: Try to change this line in your provided PHP code:

private $_HOST = "localhost";

with:

private $_HOST = "127.0.0.1";
Community
  • 1
  • 1
TinyProton
  • 110
  • 6
  • Okay, so this narrows it down to the php, Could we look into the php i provided? –  Aug 13 '13 at 01:16
  • Wow i changed the host to 127.0.0.1 and it works now... Why was that causing delay? –  Aug 13 '13 at 01:35