1

I've read a bunch of questions and copied my code from the codex, but still no luck. I get this error message when I hit my submit button:

enter image description here

my php functions and JS looks like this:

function enqueue_admin_settings_js($hook) {
  global $bblm_settings;

  if ( $hook != $bblm_settings )
    return;

  wp_enqueue_script('admin_settings_js', plugin_dir_url( __FILE__ ).'admin_settings.js', array('jqQ'));

}
add_action('admin_enqueue_scripts','enqueue_admin_settings_js');

function bblm_process_ajax(){
  echo "This is my response";
  var_dump($_POST);
  die();
}
add_action('wp_ajax_bblm_results', 'bblm_process_ajax');

The used jqQdependency is me using jquery-3.3.1.min.jsbut I have also tried with the wordpress own jquery I still get the same error. Here is how I register and enqueue the script:

function enqueue_my_scripts_jq($hook) {
  global $bblm_settings;
  wp_register_script(
    'jqQ',
    plugin_dir_url( __FILE__ ).'jquery-3.3.1.min.js'
    );

  if ( $hook == $bblm_settings ){
    wp_enqueue_script('jqQ');
    }

}
add_action('wp_enqueue_scripts','enqueue_my_scripts_jq');
add_action('admin_enqueue_scripts','enqueue_my_scripts_jq');

and at last here is my JS wich is located in the file admin_settings.js enqueued above.

$(document).ready(function ($) {
    //alert('ready');
    $('#team-form').submit(function () {
      var data = {
        action: 'bblm_result',
      };
      $.post(ajaxurl, data, function (response) {
        alert(response);
      });

      return false;
    });
  });

Again, I have tried using jQueryinstead of $ when using the Wordpress own jquerydependency, but it doesn't make a difference.

the PHP Code is inside my main PHP file for my plugin, just saying because I've read an older question where this wasn't the case and the reason for it not working. This is not the case here.

1

damn it! I just found it... my JS says action: 'bblm_result' but my PHP function is looking for wp_ajax_bblm_results ... there go my 3hours of searching

  • 1
    Have you considered using the easier REST API? In this scenario you'd have gotten a message saying the endpoint didn't exist, rather than a cryptic 400 http error code – Tom J Nowell Mar 24 '18 at 1:22
  • 1
    This isn't right. If the action in JS was bblm_results then the correct hook is in fact wp_ajax_bblm_results. Your problem seems to be that you're not setting ajaxurl. – Jacob Peattie Mar 24 '18 at 2:52
  • @TomJNowell haven't worked with the WP REST API yet. But I will give it a try. @JacobPeattie the ajaxurl is always defined in the admin header and points to admin-ajax.php. You have misread my JS just like I did. The JS says bblm_result without a S at the end. The add_actionfunction inside my PHP tho is looking for wp_ajax_bblm_results with a S at the end. cheers 🍻 – b101 Mar 24 '18 at 20:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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