A javascript function that simply runs a php function on the plugin

When an action made by an user, I want to run a php function from the plugin. I checked this link but I’m still pretty confused about it. https://codex.wordpress.org/AJAX_in_Plugins

Here is the related JS code I wrote.

$(function() {
      var data = {
        'nothing': 'nothing'
        'whatever': ajax_object.we_walue
      }
      document.getElementById("getPhp").onclick = function() {myFunction()};
      function myFunction() {
      jQuery.post(ajax_object.ajax_url, data, function(response) {
      alert('It works');
});
});

Actually, I don’t really need to pass anything, I made the variable up for just filling the jquery.post function’s “data” parameter.
Then in the php file:

function button_js(){
wp_enqueue_script( 'getPhp', plugins_url( '/mycode.js', __FILE__ ),array('jquery'));
}
 add_action('the_post', 'button_js');

add_action('wp_ajax_my_action', 'back_php');
function back_php()
{
    //I want this function to be called.   
}

But it’s not working, the alert box never pops up. Where is the issue?

Read more here: A javascript function that simply runs a php function on the plugin

Leave a Reply

Your email address will not be published. Required fields are marked *