0

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();
}
4
  • url : url needs to be the ajax url. See notes in codex.wordpress.org/…
    – shanebp
    Aug 20, 2015 at 15:23
  • i define url like var url = jQuery(this).attr("action");
    – wp Picture
    Aug 20, 2015 at 16:07
  • i edit to direct link like url : "http://www.domainname.net/wp-admin/admin-ajax.php", and still show me 0
    – wp Picture
    Aug 20, 2015 at 16:15
  • Double check the css classes and html part that is targeted. Had the same problem before two days, worked about two hours to fix it, and was only for one space.
    – emilushi
    Aug 22, 2015 at 20:53

1 Answer 1

1

A 0 status from admin-ajax.php indicates that no "action" is being parsed.

When you enqueue the script you are assigning a handle, this handle needs to be used when you use wp_localize_script and additionally you need to assign the url. https://codex.wordpress.org/Function_Reference/wp_localize_script

wp_register_script( 'some_handle', 'path/to/myscript.js' );

wp_localize_script( 'some_handle', 'object_name', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) );

wp_enqueue_script( 'some_handle' );

Then for the JS correctly call the URL

    jQuery.ajax({
                                url : object_name.ajax_url,
                                type : "POST",
                                dataType: "json",
                                data : {
                                    data : data,
                                    action : "bakkah_register_session"
                                },
                                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);            
                                }
                            });

Also in your PHP you need to actually return something from the function for jQuery to process and whatever you return needs to be a json_encoded array.

    $whatever['type'] = 'Success';
    $whatever['message'] = 'Some returned msg string';  

return json_encode( $whatever );

Within your jQuery script you would then refer to these values as response.type and response.message allowing you to perform checks....

    success : function(response){
       if( response.type == 'success')    {
            // do something
       }
       else    {
            // Failed, do something else
       } 
    }                           
2
  • I do same your code and still show me the 0
    – wp Picture
    Aug 21, 2015 at 6:46
  • 1
    We'll need to see your updated code in full including the wp_enqueue_script and wp_localize_script hooks
    – Mike
    Aug 21, 2015 at 14:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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