我试图传递变量时,在链接用户点击single.php
页在WordPress
这里是我的代码
<a href='#' id='8' class='clickme' style='font-weight:bold;color:white;font-size:16px;'>";
在function.php
,我把这个代码:
/////////////////////////////////////////////////////////
add_action('wp_head', 'myajaxcall');
function myajaxcall(){ ?>
<script type="text/javascript">
jQuery(document).ready(function($) {
jQuery(".clickme").click(function() {
// We'll pass this variable to the PHP function example_ajax_request
var id = $('.clickme').attr('id');
// This does the ajax request
$.ajax({
url: <?php echo admin_url('admin-ajax.php'); ?>,
data: {
'action':'get_sidebar_request',
'page_id' : id
},
// beforeSend: function (jqXHR, settings)
// {
// alert(url = settings.url);
//},
success:function(data){
alert(data);
},
error: function(errorThrown){
//alert(errorThrown);
}
});
});
});
</script>
<?php }
add_action('wp_ajax_get_sidebar_request', 'idAjax');
add_action('wp_ajax_nopriv_get_sidebar_request', 'idAjax');
function idAjax()
{
require(get_template_directory() . '/single.php');
}
/////////////////////////////////////////////////////////////////////
并在文件`single.php
,当我想获得传递的变量,我把这个代码:
$page_id=0;
$page_id = $_REQUEST['page_id'];
echo "page_id=".$page_id;
但该变量未传递到single.php
。我试着回应它,没有任何反应。
你能帮我吗?
当我打印var_dump($ _ POST);在single.php我得到空。 – user3470599
当我打印var_dump($ _ POST);在single.php我得到空。当我看到ff - >网络我没有看到一个调用url页面ajax-admin.php?page_id = 8我不是那个值发送到ajax-admin.php的值在所有 – user3470599