0

I've created a custom post template for a front-end question bank/quiz in which I have two elements:

  1. A div which displays question text
  2. A button when pressed should trigger an ajax query to get the next question from a database

Code in functions.php

//Load ajax object for question bank
add_action('wp_enqueue_scripts', 'my_enqueue');
function my_enqueue(){
    wp_enqueue_script( "question-refresh", get_stylesheet_directory_uri() . "/js/question-refresh.js", array("jquery") , null, true);
    $question_nonce = wp_create_nonce( 'question' );
    wp_localize_script(
        'question-refresh',
        'my_ajax_obj',
        array(
            'ajax_url' => admin_url( 'admin-ajax.php' ),
            'nonce' => $question_nonce,
        )
    );
    
}

add_action('wp_ajax_nopriv_get_question', 'get_question_from_database');
add_action('wp_ajax_get_question', 'get_question_from_database');

function get_question_from_database(){
    check_ajax_referer( 'question' ); //Checks nonce
    echo "<script type='text/javascript'>alert('SCRIPTS LOADED');</script>";
    $question_number = rand(1,4);
    $query = "SELECT * FROM `questions`
                WHERE question_number = $question_number";
    $result = $mysqli->query($query) or die($mysqli->error.__LINE__);
    $question = $result->fetch_assoc();
    echo json_encode(array("success" => true, "question-text"=>$question['question_text']));
    exit;
}

The code I have in my custom post template is as following:

<main>
    <div class="container">
        <div>Question 1 of 5</div>
        <p id="question-container">
        </p>
        <button id="next-question" type="button">Next</button>
    </div>
</main>

I have a separate js file stored in my-child-theme/js/question-refresh.js I think I have isolated where the issue is coming from. I have obviously done something wrong with the jQuery.post as it doesn't seem to be running. I am new to jQuery and have just been following other questions and tutorials. Would really appreciate if someone could spot what I am doing wrong. Many thanks!

jQuery(document).ready(function($) {
    
    $("#next-question").click( function() {
        console.log("Hello");
        $.post(
            my_ajax_obj.ajax_url, 
            { 
                _ajax_nonce: my_ajax_obj.nonce, 
                "action": "get_question"
                
            }, 
            function(response) {
                console.log("Hello");
                console.log(response);
                $("#question-container").text(response.question-text);
        });
    });
});

1 Answer 1

1

There's two problems that I see.

Firstly, your add_action('wp_ajax_nopriv_get_question',... code is inside your template, but your template is not going to be loaded when the request is sent to wp-admin/admin-ajax.php, which does not load templates. The callback code and the code that hooks it need to go in your theme's functions.php file.

Secondly, you have only hooked the callback for logged-out users (nopriv_). This means that the code will not work when you are logged in. You need to hook for both logged-in and logged-out users:

add_action('wp_ajax_get_question', 'get_question_from_database');
add_action('wp_ajax_nopriv_get_question', 'get_question_from_database');
6
  • Thanks for pointing that out. I made the necessary changes and have updated the post with the code as it stands. I am getting a POST 500 error now. Could it be something to do with the way I pass the SQL to JSON? Commented Feb 1, 2021 at 2:36
  • Also that line echo "<script type='text/javascript'>alert('SCRIPTS LOADED');</script>"; within get_question_from_database() doesn't throw an alert leading me to believe that the function isn't being called at all. Is that a fair deduction? Commented Feb 1, 2021 at 2:42
  • No, it's not. Ajax requests are handled on the server in the back-end. No scripts are going to run. Commented Feb 1, 2021 at 2:46
  • Ok thanks for clearing that up! What's the 'right' way to debug an ajax request handled on the server? I am having issues with a POST 500 error now and have no clue why it's happening. Would be open to any pointers. Commented Feb 1, 2021 at 2:54
  • A 500 error suggests a PHP error. The details should be in your error log. If it's not in /error_log your host will know where to look. Commented Feb 1, 2021 at 4:46

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.