drema2014 2018-03-23 14:28
浏览 74
已采纳

如何纠正将php变量传递给ajax调用?

I have three files for this. My main php file, functions.php, then my js file.

How do you pass this php variable to js? Here is my code for my MAIN PHP FILE

function ccss_show_tag_recipes() {

global $post;
global $wp;

$html = '';

$url = home_url( $wp->request );
$path = parse_url($url, PHP_URL_PATH);
$pathFragments = explode('/', $path);
$currentPage = end($pathFragments); //I WANT TO PASS THIS TO MY JS FILE


$term_tags = get_terms('cp_recipe_tags'); 
$term_cats = get_terms('cp_recipe_category');

$recipetype = array();
$recipecat = array();

foreach($term_tags as $term) {
    $recipetype[] = $term->slug;
}

foreach($term_cats as $term) {
    $recipecat[] = $term->slug;
}


if ( in_array( $currentPage , $recipecat  ) ) {

        $html = ccss_load_phases($html,$currentPage);

    }

}

Here is the function for for css_load_phases();

function ccss_load_phases($html = '',$currentPage){
     //CODE HERE etc

    $html .= '<div id="btn-wrapper-'.$termid.'" class="btn-wrapper"><a id="'.$termid.'" class="load-more-btn" href="#">Load more recipes</a></div>';    

}

That code above is to trigger the Ajax call, All I can get is the button id.

Here is the AJAX file code:

(function($) {
    $(document).on( 'click', 'a.load-more-btn', function( event ) {


    var a = $(this).attr('id'); // I CAN ONLY GET THE ID SINCE IT WAS TRIGGERED BY BUTTON CLICK, how to include $currentPage?

    var button = $(this),
            data = {
            'action': 'ajax_loadmore',
            'termID': a
        };



    event.preventDefault();

        $.ajax({
            url: ajaxloadmore.ajaxurl,
            data: data, 
            type: 'post',
            beforeSend : function () {
                button.text('Loading...'); // change the button text, you can also add a preloader image
            },
            success: function( result ) {

                 //console.log(data.post_gallery_imgs);

                $('#cooked-recipe-list-'+a+' ').append(result).fadeIn();
                c
                //alert( result );
            },

            complete: function() {
                button.remove(); // if no data, remove the button as well
                //$('body').find('a#'+a+'').remove();
            }
        })
    })

})(jQuery);

How Do I pass that $currentPage php variable to my Ajax/Js file? so I can use it here:

add_action( 'wp_ajax_nopriv_ajax_loadmore', 'my_ajax_loadmore' );
add_action( 'wp_ajax_ajax_loadmore', 'my_ajax_loadmore' );

function my_ajax_loadmore() {


    $termID = $_POST['termID']; //ONLY THE BUTTON ID WAS RETRIEVED

}
  • 写回答

2条回答 默认 最新

  • doufendi9063 2018-03-23 14:41
    关注

    In your function ccss_load_phases, include an input which is hidden.

    function ccss_load_phases($html = '',$currentPage){
    
        $html .= '<div id="btn-wrapper-'.$termid.'" class="btn-wrapper">
            <a id="'.$termid.'" class="load-more-btn" href="#">Load more recipes</a>
            <input type="hidden" id="currentPage" value="'.$currentPage.'" />
              </div>';    
    
    }
    

    Now you should be able to access this in your javascript by referencing:

    $('#currentPage').val(); or is it value();

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?