WordPress Ajax Not Firing – WordPress Development Stack Exchange

Question

I’ve been out of the WordPress game for a while now and I’m just getting back into it. I’m trying to practice calling a PHP function via Ajax but I’m not able to get the Ajax call to even fire (when checking in Google Chrome Developer).

I’ve got a funny feeling that this may have something to do with where I am adding the code. Currently I’m just adding all this in a HTML block verbatim as per below but I’ve got a funny feeling that this may be the issue?

Do I need to add the javascript via a plugin or similar?

My simple example is as follows:

<div id="output_div">
   <p>This is where output should go</p>
</div>

<select name="player_ID" id="player_ID">
  <option value="player1">player1</option>
  <option value="player2">player2</option>
</select>

<script type="text/javascript">
jQuery(document).ready(function ($) {
    var valueCheck;
    jQuery("#player_ID").on("change", function () {
        jQuery.ajax({
            type: "POST",
            url: "/wp-admin/admin-ajax.php",
            data: {
                action: "practice_ajax_function"
            },
            success: function (output) {
                jQuery("#output_div").html(output);
            }
        });
    }).change();
});
</script>

0
Morts81 4 weeks 2022-07-23T19:09:09-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse