Simple AJAX example
-
Hello,
I’m new to AJAX and I cannot get it running in a wordpress installation. I suppose that I’m doing something wrong… May you help me please? Thank you in advance.
<?php
$ajax_nonce = wp_create_nonce( "dsfls-nonce" );
?>
<script type="text/javascript" >
jQuery(document).ready(function($) {
$('#temp_m17').change(function(){
var id_tempm17 = $(this).val();
var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
var data = {
action: 'my_action',
security: '<?php echo $ajax_nonce; ?>',
id_temp: id_tempm17
};
$.post(ajaxurl, data, function(response) {
$('#designers_m17').html( response );
});
});
});
</script><?php
add_action( 'wp_ajax_my_action', 'my_action_callback' );
add_action( 'wp_ajax_nopriv_my_action', 'my_action_callback' ); // need this to serve non logged in usersfunction my_action_callback() {
global $wpdb; // this is how you get access to the databasecheck_ajax_referer( 'dsfls-nonce', 'security' );
echo 'hello';
die(); // this is required to terminate immediately and return a proper response
} ?>
- The topic ‘Simple AJAX example’ is closed to new replies.