I am relatively new to jQuery and AJAX in particular. I have a small issue with the return value always being 0, though I think this is actually the success message and it's not returning anything.
I have scoured the Google-verse and I have the die() function on the PHP callback and I believe the add_actions are correct.
I am working on a local host, though I doubt that affects it and this is all in the admin, not front end. I also checked that the js is enqueued and localised.
I also tested out the basic AJAX from http://codex.wordpress.org/AJAX_in_Plugins and it also returned 0, which makes me wonder if it is something other than the code outlined below.
Right now I am just trying to make it send something back to the jQuery. Any help would be appreciated.
The jQuery
<script>
jQuery(document).ready(function() {
jQuery(".bakkah_session_registrtation").click(function(e){
e.preventDefault();
jQuery("#session_modal").slideToggle();
});
jQuery("#session_registration_form").submit(function(e){
e.preventDefault();
jQuery("#bakkah_submit_button").attr("disabled", "disabled");
var url = jQuery(this).attr("action");
var data = jQuery(this).serialize();
var obj = jQuery(this);
jQuery.ajax({
url : url,
type : "POST",
data : {action : "bakkah_register_session", data : data},
success : function(response){
jQuery(".message_register_bakkah").html(response);
jQuery("#bakkah_submit_button").removeAttr("disabled");
obj[0].reset();
console.log (response);
},
error: function(response) {
console.log(response);
}
});
});
});
jQuery("#course_select").change(function(){
var course = jQuery(this).val();
if(course == ""){
jQuery("#session_select").attr("disabled", "disabled");
jQuery("#session_select #default_session").attr("selected", "selected");
}else{
jQuery("#session_select").removeAttr("disabled");
jQuery("#session_select option").each(function(){
jQuery(this).prop("disabled", true);
});
jQuery("#session_select option").each(function(){
if(parseInt(jQuery(this).attr("var")) == parseInt(course)){
jQuery(this).prop("disabled", false);
}
});
}
});
</script>
The PHP:
add_action('wp_ajax_bakkah_register_session', 'bakkah_register_session');
add_action('wp_ajax_nopriv_bakkah_register_session', 'bakkah_register_session');
function bakkah_register_session(){
global $wpdb;
$data = array();
parse_str($_POST['data'], $data);
$insert = $wpdb->insert(
$wpdb->prefix."bakkah_course_session_registration",
$data
);
wp_die();
}
var url = jQuery(this).attr("action");
url : "http://www.domainname.net/wp-admin/admin-ajax.php",
and still show me 0