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>
Sie haben einen kleinen Tippfehler '' wp_ajax_norpiv_my_custom_fixture_template' wp_ajax_nopriv_my_custom_fixture_template' sein muss. – Benoti
Danke. Wie kann ich den Inhalt von $ competition_id anstelle von ID anzeigen? –