0

I'm trying to implement the Jquery .ajax method to simplify the ajax in my website.

Here is the function I'm working with:

function autoComplete(q, succ)
{
    $.ajax({type:"GET",
        url: "search.php",
        data: "q="+q,
        success: succ
    }); 
}

$('input#auto_results').live('keyup', function() {

    var text = $('input#auto_results').val();       

    autoComplete(text,
        function(data) 
        { 

        alert(data);

        }); 
}); 

The response on the PHP page is simply:

echo "response";

So I figure that it should alert the response when the function is called, on 'keyup'. Sadly, nothing occurs. I must be doing something wrong, I am just not sure what it is.

6
  • Have you checked firebug? Is the request sent, is data received? Are there any errors? Is the PHP page and the page containing this JS under same domain? Commented Jun 16, 2011 at 8:36
  • are there any JS errors? Using firebug or chrome dev tools can you see if the ajax request is being started? Commented Jun 16, 2011 at 8:37
  • try doing alert("Response: '"+data+"'") - this way you will at least be alerted when response is empty (otherwise empty alert doesn't open at all) Commented Jun 16, 2011 at 8:40
  • Thank you for that suggestion! please post as an answer Commented Jun 16, 2011 at 8:41
  • I checked and I made the mistake of thinking that the reference to the php page needed to be made from the folder where the JS file that makes the Ajax calls are. I just referenced to dir/search.php and it worked Commented Jun 16, 2011 at 8:42

1 Answer 1

3

is "keyup" event firing? do following.

$('input#auto_results').live('keyup', function() {

    var text = $('input#auto_results').val();       
    alert("Keyup event is firing");
    autoComplete(text,
        function(data) 
        { 

        alert(data);

        }); 
}); 

if event is firing. then see firebug console tab

or put error function callback on your code:

function autoComplete(q, succ)
{
    $.ajax({type:"GET",
        url: "search.php",
        data: "q="+q,
        error:function(request, textStatus, err){
           alert(request.statusText);
        },
        success: succ
    }); 
}

you may get near to error.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.