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条)

报告相同问题?

悬赏问题

  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用