2016-11-23 4 views
0

Ich bin sehr neu zu Ajax.In Ajax-Funktion, kann ich nicht zeigen, die Variable $competition_id Inhalt.Ich verwende folgende Codes, aber es zeigt nur die ID.Wie wird der Variablenwert in der Ajax-Callback-Funktion angezeigt?

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-Code:

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 Codes:

<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

Sie haben einen kleinen Tippfehler '' wp_ajax_norpiv_my_custom_fixture_template' wp_ajax_nopriv_my_custom_fixture_template' sein muss. – Benoti

+0

Danke. Wie kann ich den Inhalt von $ competition_id anstelle von ID anzeigen? –

Antwort

0

Ihre PHP-Funktion gibt nichts zurück. Daher muss das jQuery-Skript nichts an .new-fixture anhängen.

Sie können in der Ajax-Antwort so etwas wie

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

dann tun, werden Sie in der Lage sein, die JSON-Antwort zu analysieren.

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

Hoffe, dass es einige Hinweise gibt ...

0

ich gelöst habe es von mir folgende Codes:

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