cgee cgee - 6 months ago 41
Ajax Question

AJAX response always 0

I try to develop a wordpress plugin.
Now I want to make a AJAX call.

I've two files.

First file is the file of my template:

add_action( 'admin_footer', 'dmd_action_javascript' );
?>

<?php function dmd_action_javascript() { ?>
<script type="text/javascript">
jQuery(document).ready(function() {
//click event f�r submit button
jQuery('.dmd_admin_button').click(function () {

//Daten von den HTML Feldern in JS-Vars �bersetzen
var post = jQuery('input[name=dmd_fast_post]');
var page = jQuery('input[name=dmd_fast_page]');
var searchbox = jQuery('input[name=dmd_fast_searchbox]');
var formidable = jQuery('input[name=dmd_fast_formidable]');
var key = jQuery('input[name=dmd_fast_key]');

if ( jQuery( post ).prop( "checked" ) )
post.val(1);
else
post.val(0);

if ( jQuery( page ).prop( "checked" ) )
page.val(1);
else
page.val(0);

if ( jQuery( searchbox ).prop( "checked" ) )
searchbox.val(1);
else
searchbox.val(0);

if ( jQuery( formidable ).prop( "checked" ) )
formidable.val(1);
else
formidable.val(0);

var data = 'post=' + post.val() + '&page=' + page.val() + '&searchbox=' + searchbox.val() + '&formidable=' + formidable.val() + '&key=' + key.val();
jQuery.ajax({
type:"POST",
url: ajaxurl, // our PHP handler file
data: { action: "my_backend_action", data:data },
success:function(data){
alert(data);
},
error: function(errorThrown){
alert(errorThrown);
}

});
return false;

});
});
</script>
<?php } ?>


my second file in my plugin should response some values of the AJAX call.:

public function set_actions(){
add_action( 'admin_enqueue_scripts', array($this, 'dmd_custom_style_load'), 99 );
add_action( 'wp_enqueue_scripts', array($this, 'dmd_enqueue_child_theme_styles'), 99);
add_action( 'admin_menu', array($this, 'dmd_register_adminmenu'));
add_action( 'wp_before_admin_bar_render', array($this, 'dmdPages'));
add_action('wp_ajax_my_backend_action_callback', array($this, 'my_backend_action_callback'));
add_action( 'wp_ajax_nopriv_my_backend_action_callback', array($this, 'my_backend_action_callback'));
}

public function my_backend_action_callback() {
global $wpdb; // this is how you get access to the database

$whatever = intval($_POST['data']);

echo $whatever;
exit(); // this is required to terminate immediately and return a proper response
}


Sorry, but I read all questions and answers in stackoverflow and nothing helps me.

I can't find my fault. Can somebody tell me what is the issue?

EDIT:

In firebug my post looks like this:


action my_backend_action data
post=1&page=1&searchbox=0&formidable=0&key=27dda19e85378bb8df73fa3f2806a30c

Answer
jQuery.ajax({
                type: 'POST',
                url: '<?php echo admin_url( 'admin-ajax.php'); ?>',
                data: {"action": "validateID","post": post.val(), "page": page.val() , "searchbox": searchbox.val(), "formidable": formidable.val(), "key": key.val()},
                success: function(data){
                     alert(data);
    }
});

Call ajax as above amd then write following and see now print anything or not

function validateID(){
    echo $response = $_REQUEST['post'];
    die();
}
add_action( 'wp_ajax_validateID', 'validateID' );
add_action( 'wp_ajax_nopriv_validateID', 'validateID' );
Comments