0

I'm receiving the variable to JavaScript, but not getting it back in PHP, and I think Its not being coded correctly in the JavaScript:

function glitch_player_display(mix_name) {
  alert(mix_name);
  throb.start();
  jQuery.ajax({
    type: 'POST',
    url: ajaxglitch_playerajax.ajaxurl,
    data: {
      action: 'ajaxglitch_player_ajaxhandler',
      mix_name: mix_name
    },
    success: function(data, textStatus, XMLHttpRequest) {
      throb.stop();
      var showglitchplayer = '#showglitchplayer';
      jQuery(showglitchplayer).html('');
      jQuery(showglitchplayer).append(data);
    },
    error: function(MLHttpRequest, textStatus, errorThrown) {
      alert(errorThrown);
    }
  });
}

This is the PHP:

add_action( 'wp_ajax_nopriv_ajaxglitch_player_ajaxhandler', 'ajaxglitch_player_ajaxhandler' );
add_action( 'wp_ajax_ajaxglitch_player_ajaxhandler', 'ajaxglitch_player_ajaxhandler' );

function ajaxglitch_player_ajaxhandler($arguments){
  var_dump($arguments);
}

returns empty.

Thanks and have a great one.

9
  • What player are you using? What are you trying to do in detail? Please let us know all the details ;)
    – Sven
    Jun 2, 2014 at 21:45
  • @Sven I'm actually sending the variable to a python script. Echonest Remix. It's pretty sweet.
    – MikeiLL
    Jun 2, 2014 at 21:52
  • most of the code is posted here on gitHub
    – MikeiLL
    Jun 2, 2014 at 21:59
  • Try to change url: ajaxglitch_playerajax.ajaxurl to url: ajaxglitch_playerajax; I'm getting the request, but there seems to be another problem...
    – Sven
    Jun 2, 2014 at 22:35
  • that causes a "not found" js error... and $arguments is still empty back in php.
    – MikeiLL
    Jun 2, 2014 at 22:43

1 Answer 1

0

The AJAX callback doesn't take any parameters, you have to get them from the request using $_GET/$_POST/$_REQUEST (depending on how you have your javascript set up). This means that to get get mix_name from your POST you would use $_POST['mix_name']. Including the add_action( 'wp_ajax_... calls from your code would be good if you have future questions as well, I had to go to the GitHub page to see how you had it set up - you're also missing single quotes around add_action( 'wp_enqueue_scripts', 'ajaxglitch_player_enqueuescripts' ); which PHP can figure out, but it will try it as a constant first. This is what the config should look like:

function ajaxglitch_player_enqueuescripts() {
    wp_enqueue_script('ajaxglitch_player', glitch_player_URL.'/js/glitch_player.js', array('jquery'));
    // access ajax url on the front end as ajaxglitch_playerajax.ajaxurl
    wp_localize_script( 'ajaxglitch_player', 'ajaxglitch_playerajax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
}
add_action( 'wp_enqueue_scripts', 'ajaxglitch_player_enqueuescripts' );

// front end ajax handler
add_action( 'wp_ajax_nopriv_ajaxglitch_player_ajaxhandler', 'ajaxglitch_player_ajaxhandler' );
// admin ajax handler
add_action( 'wp_ajax_ajaxglitch_player_ajaxhandler', 'ajaxglitch_player_ajaxhandler' );

function ajaxglitch_player_ajaxhandler(){
    $mix_name = isset( $_POST['mix_name'] )? $_POST['mix_name'] : false;
    var_dump( "The mix_name is $mix_name" ); // this will probably break the AJAX response
    error_log( "The mix_name is $mix_name" ); // write it to the error_log too.
}
8
  • Thank you, amigo. tripplesharp! I'm planning on having more questions in the future. And soon. I would only want to write out to the error log for dev purposes though, correct?
    – MikeiLL
    Jun 2, 2014 at 23:30
  • That's a personal preference - one downside is that it can be hard to clean up all of your debug statements once you are ready to move to prod. Another option would be to call your own my_debug() function and put the error_log() inside it, then control it all with a if ( $debug ) - you just have to set $debug somewhere. Jun 3, 2014 at 0:13
  • As a personal preference I also like to use classes for my plugins to keep the namespace cleaner and allow for shorter action/function names to keep it readable. No AJAX callbacks but it's shorter so you can see the pattern in this plugin I wrote - justin.ag/technology/wordpress/wordpress-plugins/… - with add_action() for you being like add_shortcode() in this plugin. Jun 3, 2014 at 0:18
  • So $debug would be hardcoded to either true or false? Or would it be aligned with the wordpress debug config?
    – MikeiLL
    Jun 3, 2014 at 2:04
  • However you wanted to do it - I would probably use a constant in wp-config.php if I was going to hardcode it. Typically i just go wild and then search the code once I'm done and remove anything that isn't valid. Jun 3, 2014 at 3:21

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.