最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Admin Ajax is returning 0 and not Insert data

programmeradmin0浏览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 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();
}

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/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();
}
Share Improve this question asked Aug 20, 2015 at 15:10 wp Picturewp Picture 1 4
  • url : url needs to be the ajax url. See notes in codex.wordpress/… – shanebp Commented Aug 20, 2015 at 15:23
  • i define url like var url = jQuery(this).attr("action"); – wp Picture Commented Aug 20, 2015 at 16:07
  • i edit to direct link like url : "http://www.domainname/wp-admin/admin-ajax.php", and still show me 0 – wp Picture Commented 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 Commented Aug 22, 2015 at 20:53
Add a comment  | 

1 Answer 1

Reset to default 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/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
       } 
    }                           
发布评论

评论列表(0)

  1. 暂无评论