0

I'm loading Contact Form plugin with an Ajax request. It is correctly loaded, but when trying to submit the form, I get a blank page with a 0 as unique response. I'm guessing it has something to do with the Event Handler, but don't know how to solve it. Any idea? This is my ajax code in functions.php (here I'm omitting the contact-page template call in Ajax callback, because it works):

function theme_name_scripts() {
    wp_enqueue_script( 'mi-script-ajax',get_bloginfo('stylesheet_directory') . '/js/ajax-search.js', array( 'jquery' ) );
    wp_localize_script( 'mi-script-ajax', 'MyAjax', array( 'url' => admin_url( 'admin-ajax.php' ) ) );
}

function ajax_callback() {  
//Submenu
echo '<div id="submenu">';
if( $sPostType !== '' ) {
    if( $sPostType == -1 ) {
        $allCategories = get_categories();
        foreach( $allCategories as $cat ) {
            echo '<li class="type-category"><a href="' . get_permalink( $cat->cat_ID ) . '">' . $cat->name . '</a></li>'; 
        }
    } else {

        global $wpdb;
        // set the target relationship here
        $post_type = $sPostType;
        $taxonomy = 'category';

        $terms_ids = $wpdb->get_col( $wpdb->prepare( "
            SELECT
                tt.term_id
            FROM
                {$wpdb->term_relationships} tr,
                {$wpdb->term_taxonomy} tt,
                {$wpdb->posts} p
            WHERE 1=1
                AND tr.object_id = p.id
                AND p.post_type = '%s'
                AND p.post_status = 'publish'
                AND tr.term_taxonomy_id = tt.term_taxonomy_id
                AND tt.taxonomy ='%s'
            ", $post_type, $taxonomy ) );

        $terms = get_terms( $taxonomy, array(
            'include' => $terms_ids,
            'orderby' => 'name',
            'order' => 'ASC'
        ) );


         //$aTypeCategories = get_categories( array( 'post_type' => $type, 'number' => '10'  ) );
         foreach( $terms as $cat ) {
            echo '<li class="type-category"><a href="' . get_permalink( $cat->term_id ) . '">' . $cat->name . '</a></li>'; 
         }
    }
} else {
    echo '<li class="type-category" style="color: transparent">Sin categorías</li>'; 
}
echo '</div>';
}

add_action( 'wp_enqueue_scripts', 'theme_name_scripts' );
add_action('wp_ajax_ajax', 'ajax_callback');
add_action('wp_ajax_nopriv_ajax', 'ajax_callback');

This is the full ajax_callback function:

function ajax_callback() {
    $pageId = 0;
    $newSlug = '';
    $reviewSlug = '';
    $sPostType = '';
    $sTemplatePart = '';

    //Links
    if( isset(  $_POST['href'] ) ) {
        $pageId = preg_match( '/^http:\/\/.+\/\?page_id=\d+$/', $_POST['href'] ) ? substr( strrchr( $_POST['href'], '=' ), 1 ) : 'no-page' ;
        if( $pageId === 'no-page' && preg_match( '/^http:\/\/.+\/\?(news|reviews|entrevistas|radio_shows)=.+$/', $_POST['href'] ) ) {
            $newSlug = substr( strrchr( $_POST['href'], '=' ), 1 );
        }
        //$pageName = get_page_template_slug( intval( $pageId ) );
        if( $pageId !== 'no-page' ) {
            if( $pageId === '8' ) {
                $sPostType = -1;
                $sTemplatePart = 'home-ajax';
            } elseif( $pageId === '22' ) { 
                $sPostType = 'reviews';
                $sTemplatePart = 'reviews-rvw-ajax';
            } elseif( $pageId === '6' ) {
                $sPostType = 'news';
                $sTemplatePart = 'noticias-news-ajax';
            } elseif( $pageId === '109' ) {
                $sPostType = 'entrevistas';
                $sTemplatePart = 'entrevistas-ajax';
            } elseif( $pageId === '112' ) {
                $sPostType = 'radio-shows';
                $sTemplatePart = 'radio-shows-ajax';
            } elseif( $pageId === '412' ) {
                $sTemplatePart = 'podcasts-ajax';
            } elseif( $pageId === '415' ) {
                $sTemplatePart = 'contacto-ajax';
            }

        } elseif( $newSlug !== '' ) {
            if( preg_match( '/^http:\/\/.+\/\?news=.+$/', $_POST['href'] ) ) {
                 $post_type = 'news';
            } elseif( preg_match( '/^http:\/\/.+\/\?reviews=.+$/', $_POST['href'] ) ) {
                 $post_type = 'reviews';
            } elseif( preg_match( '/^http:\/\/.+\/\?entrevistas=.+$/', $_POST['href'] ) ) {
                 $post_type = 'entrevistas';
            } elseif( preg_match( '/^http:\/\/.+\/\?radio_shows=.+$/', $_POST['href'] ) ) {
                 $post_type = 'radio_shows';
            } elseif( preg_match( '/^http:\/\/.+\/\?podcasts=.+$/', $_POST['href'] ) ) {
                 $post_type = 'podcasts';
            } else {
                //die();
            }
            $sTemplatePart = 'single-ajax';
            //get_template_part( 'single-ajax' );
        }

    //Siguiente y Anterior
    } elseif( isset( $_POST['cadenaAll'] ) && ( isset( $_POST['cadenaPrev'] ) || isset( $_POST['cadenaNext'] ) ) ) {
        if( isset(  $_POST['pageName'] ) ) {
            if( $_POST['pageName'] === 'reviews' ) {
                $sPostType = 'reviews';
                $sTemplatePart = 'next_reviews-rvw-ajax';
            }
            elseif( $_POST['pageName'] === 'news' ) {
                $sPostType = 'news';
                $sTemplatePart = 'next_noticias-news-ajax';
            }
            elseif( $_POST['pageName'] === 'entrevistas' ) {
                $sPostType = 'entrevistas';
                $sTemplatePart = 'next_entrevistas-ajax';
            }
            elseif( $_POST['pageName'] === 'radio_shows' ) {
                $sPostType = 'radio-shows';
                $sTemplatePart = 'radio-shows-ajax';
            }
            elseif( $_POST['pageName'] === 'radio_shows' ) {
                $sPostType = 'podcasts';
                $sTemplatePart = 'podcasts-ajax';
            }
        }
    }


    //Submenu
    echo '<div id="submenu">';
    if( $sPostType !== '' ) {
        if( $sPostType == -1 ) {
            $allCategories = get_categories();
            foreach( $allCategories as $cat ) {
                echo '<li class="type-category"><a href="' . get_permalink( $cat->cat_ID ) . '">' . $cat->name . '</a></li>'; 
            }
        } else {

            global $wpdb;
            // set the target relationship here
            $post_type = $sPostType;
            $taxonomy = 'category';

            $terms_ids = $wpdb->get_col( $wpdb->prepare( "
                SELECT
                    tt.term_id
                FROM
                    {$wpdb->term_relationships} tr,
                    {$wpdb->term_taxonomy} tt,
                    {$wpdb->posts} p
                WHERE 1=1
                    AND tr.object_id = p.id
                    AND p.post_type = '%s'
                    AND p.post_status = 'publish'
                    AND tr.term_taxonomy_id = tt.term_taxonomy_id
                    AND tt.taxonomy ='%s'
                ", $post_type, $taxonomy ) );

            $terms = get_terms( $taxonomy, array(
                'include' => $terms_ids,
                'orderby' => 'name',
                'order' => 'ASC'
            ) );


             foreach( $terms as $cat ) {
                echo '<li class="type-category"><a href="' . get_permalink( $cat->term_id ) . '">' . $cat->name . '</a></li>'; 
             }
        }
    } else {
        echo '<li class="type-category" style="color: transparent">Sin categorías</li>'; 
    }
    echo '</div>';
    if( $sTemplatePart ) get_template_part($sTemplatePart);
}

And this is the Ajax code:

jQuery(document).ready(function($) {


    var linksPages = $('.menu-menu-container a');

    linksPages.on('click',function(e){
                e.preventDefault();
                jQuery.post(MyAjax.url, {action : 'ajax' ,href : $(this).attr('href') }, function(response) {
                $('#content').addClass('cortinaOut');
                setTimeout(function() {
                    $('#content').html(response).fadeIn();
                    $('#content').removeClass('cortinaOut');
                }, 500);
            });
    });

    var linksPost = $('a:not(".rpswNamePrev_2, .rpswNameNext_2, .no-ajax")')
    linksPost.live('click',function(e){
                e.preventDefault();
                jQuery.post(MyAjax.url, {action : 'ajax' ,href : $(this).attr('href') }, function(response) {
                $('#content').addClass('cortinaOut');
                setTimeout(function() {
                    $('#content').html(response).fadeIn();
                    $('#content').removeClass('cortinaOut');
                }, 500);
            });
    });


    var cadenaNext='';
    var cadenaPrev='';
    $('#ajax-form-next, .ajax-form-num').live('submit',function(e){
                e.preventDefault();
                jQuery.post(MyAjax.url, {action : 'ajax' ,cadenaNext : $(this).find('.nextPosts').val(), cadenaAll : $(this).find('.allPosts').val(), pageName : $(this).find('.pageName').val(), numPage : $(this).find('.numPage').val() }, function(response) {
                $('#content').addClass('cortinaOut');
                setTimeout(function() {
                    $('#content').html(response).fadeIn();
                    $('#content').removeClass('cortinaOut');
                }, 500);
            });
    });

    $('#ajax-form-prev').live('submit',function(e){
                e.preventDefault();
                jQuery.post(MyAjax.url, {action : 'ajax' ,cadenaNext : $(this).find('.prevPosts').val(), cadenaAll : $(this).find('.allPosts').val(), pageName : $(this).find('.pageName').val(), numPage : $(this).find('.numPage').val() }, function(response) {
                $('#content').addClass('cortinaOut');
                setTimeout(function() {
                    $('#content').html(response).fadeIn();
                    $('#content').removeClass('cortinaOut');
                }, 500);
            });
    });


});
  • Firstly, You should add die()/exit() at the end of ajax_callback. That way 0 won't be returned. I don't see any problem with this code. But I would sure check by echoing some data like "hello world" first. If that works maybe your database query aren't returning anything. – sakibmoon Jan 19 '14 at 9:52
  • Please post your JavaScript ajax code as well. 0 response mostly the common problem and it cause when your action parameter of POST/GET on JavaScript is not declared or not same as wp_ajax_action or wp_ajax_nopriv_action. – Rahil Wazir Jan 19 '14 at 13:59
  • @RahilWazir - Done – Manolo Jan 19 '14 at 14:18
  • @ManoloSalsas You posted the same extended code. I said please show us your code for javascript ajax call. – Rahil Wazir Jan 19 '14 at 16:36
  • @RahilWazir - Oh! Sorry. Here it is. – Manolo Jan 19 '14 at 16:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.