2016-11-23 151 views
0

我對Ajax非常陌生。在ajax函數中,我無法顯示變量$competition_id內容。我正在使用以下代碼,但僅顯示ID。如何在Ajax回調函數中顯示變量值?

function custom_fixture_template(){ 


    $competition_id = $_POST[ 'competion' ]; 
    echo $competition_id; 
    $args = array(
     'post_type' => array('football_fixture'), // profile and letter are CPTs 
     'posts_per_page'=>5, 
     'meta_key' => 'pb_match_date', 
     'orderby' => 'meta_value', 
     'order'  => 'DESC', 
     'tax_query' => array(
      array(
      'taxonomy' => 'competition', 
      'field' => 'id', 
      'terms' => $competition_id 
      ), 
     ), 

     'meta_query' => array(
      array(
       'key'  => 'pb_match_status', 
       'value' => 'fixture' 
      ), 
    ) 

    ); 
     $my_query = null; 
    $my_query = new WP_Query($args); ?> 
} 
add_action("wp_ajax_my_custom_fixture_template", "custom_fixture_template"); 
add_action("wp_ajax_norpiv_my_custom_fixture_template", "custom_fixture_template"); 

Ajax代碼:

jQuery(".fixture-competition").change(
       function(){ 
        jQuery.ajax({ 
         url:"<?php echo admin_url();?>admin-ajax.php", 
         type:"post", 
         data:{"action":"my_custom_fixture_template",competion:jQuery(this).val()}, 
         success:function(res){ 
           jQuery('.competition-container').remove(); 
           jQuery('.new-fixture').html(''); 
           jQuery('.new-fixture').append(res); 
         } 
        }); 

      }); 

HTML代碼:

<select name="fixture-competition" class="fixture-competition"> 
        <?php 
        $args = array(
         'type'      => 'football_fixture', 
         'child_of'     => 0, 
         'parent'     => '', 
         'orderby'     => 'name', 
         'order'     => 'ASC', 
         'hide_empty'    => 1, 
         'hierarchical'    => 1, 
         'exclude'     => '', 
         'include'     => '', 
         'number'     => '', 
         'taxonomy'     => 'competition', 
         'pad_counts'    => false); 
        $competition_fixture = get_categories($args); ?> 

        <option value="Select All">--Select--</option> 
        <?php 
        foreach ($competition_fixture as $competition) { ?> 
         <option value="<?php echo $competition->term_id; ?>"> 
          <?php $url = get_term_link($competition);?> 
         <a href="<?php echo $url;?>"><?php echo $competition->name; ?></a> 
         </option> 

        <?php 
        } 
        ?> 
       </select> 
+0

你有一個小錯字'wp_ajax_norpiv_my_custom_fixture_template'必須是'wp_ajax_nopriv_my_custom_fixture_template'。 – Benoti

+0

Thanks.But如何顯示$ competition_id的內容而不是ID? –

回答

0

你的PHP函數不返回任何東西。所以jQuery腳本沒有什麼可以附加到.new-fixture。

你可以做這樣的事情

$my_query = new WP_Query($args); // be carefull of typo (a php closing tag) 
echo json_encode($my_query); 

然後在Ajax響應,你就可以解析JSON響應。

success:function(res){ 
     var json = $.parseJSON(res); 
     console.log(json); 
     ... 
} 

希望它給你一些提示...

0

我已使用如下代碼自己解決了這個問題:

<div class="team-league-name-top"> 
    <?php 
    $competition_name = get_term($competition_id, 'competition'); 
    echo $competition_name->name; 
    ?> 
</div> 
相關問題