0

Can anyone tell me what I did wrong? I made an ajax call in wordpress to loop through data to display the results on a table.

Here is my php:

    function DisplayInfo() {

    global $wpdb;

    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];


    
        $data = $wpdb->get_results("SELECT first_name, last_name FROM students");
    
        echo "<div value='".$first_name."'>".$first_name."</div>"; // test
        foreach($data as $student)
            {
                echo '<tr>'; 
                    echo '<td>';
                    echo '<input type="checkbox" name="progress" id="progress1" value="1" tabIndex="1" onClick="ckChange(this)">'; // This line displays a button each time looped through
                    echo '</td>';
                    echo '<form>'; 
                                echo '<td>' . $student->first_name . '</td>';
                                echo '<td>' . $student->last_name . '</td>';
                    echo '</form>';
                echo '</tr>';
                
            }
}

Here is the ajax call:

echo '<script>
jQuery.ajax({
    type: "POST",
    url: "/wp-admin/admin-ajax.php",
    data: {
         "key" : {first_name, last_name},
        "action": "fetch_data" // very important
    },
    success : function (data) {
             console.log(data);
             jQuery("#result").html(data);
            }
});

</script>';

Finally, how I call the everything on my page:

echo '<div id="result"></div>';
2
  • 1
    how is DisplayInfo being called? If it did not do what you expected it to do, what did it do instead? You haven't shared the problem with us so it's difficult to know what the issue is. Also you're using first_name and last_name but I do not see them in your AJAX call, I only see a key field which is never referenced in the PHP
    – Tom J Nowell
    Oct 27, 2022 at 16:28
  • Where is this echo happening? (your ajax call portion of code) Is it late enough that jquery has been loaded before the script data has been output?
    – t31os
    Oct 27, 2022 at 22:09

1 Answer 1

0

Have you actually added this action through functions.php?

This might be of interest for you: https://developer.wordpress.org/reference/hooks/wp_ajax_action/

in your case the php function needs to be added as a hook: add_action('wp_ajax_fetch_data', 'DisplayInfo') add_action('wp_ajax_nopriv_fetch_data', 'DisplayInfo')

2
  • The action in the code posted is fetch_data. It doesn't matter what you name your callback function, only that it hooks the correct action name, or inversely that your ajax call action matches the hook your callback is attached to.
    – t31os
    Oct 27, 2022 at 22:07
  • aahh sorry I copied the wrong action name, you're right! Let me edit it
    – Humpff
    Oct 29, 2022 at 13:12

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.