2

I'm making a small game project with a leaderboard table.

I'm trying to get the leaderboard from a mysql database to display using jQuery ajax and PHP but its giving me an error message.

My syntax seems to look fine so I'm not sure what the problem is.

$("#leader-btn").click(function() {

    $.ajax({
        type: "GET",
        url: "leaderboard.php",
        dataType: "html",
        success: function(response) {
            $("#leaderboard-box").html(response);
            $("#leaderboard-box").css("display","block");
        }

    });

});
12
  • 3
    Have you included jQuery, and also have you wrapped this in $(document).ready(function() {? Commented Dec 2, 2016 at 18:25
  • 1
    @Tricky12 Event handler declarations aren't supposed to be wrapped in $(document).ready(). Commented Dec 2, 2016 at 18:28
  • 1
    @codenoir All jQuery needs to be wrapped in it, otherwise it may be declared before jQuery has loaded, and will therefor not be attached to the DOM. Hence why he would see a "function does not exist" error. It tries to call the function before jQuery has loaded. Commented Dec 2, 2016 at 18:33
  • 1
    @Tricky12 not quite, but you keep on thinking that. Commented Dec 2, 2016 at 18:34
  • 2
    One other thing to try is jQuery.ajax instead of $.ajax. If you have more libraries included than just jQuery, $ could potentially be aliased to something else in your code. Commented Dec 2, 2016 at 19:03

1 Answer 1

10

I had the same problem. It turned out that I was using jQuery slim instead of the full library.

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.