更新2/28/2012:感谢@charlietfl,这个问题在最后有一个解决方案。
我在一些JQuery脚本中有一个表单和一个AJAX调用,并且AJAX似乎正在成功执行,但是,PHP文件中的$ _POST变量仍然是空的。不知道我做错了什么。我的代码在下面评论。
主要问题涉及PHP文件。为什么PHP $ _POST变量未设置为“是”?如果我执行var_dump,它始终显示NULL。但是,我相信我是使用典型的AJAX方法手动将'removeall'设置为任意值,在本例中为'yes'。 PHP文件不应该将带有'removeall'标签的$ _POST变量设置为'yes'吗?
我希望无论我做错什么,对某人来说都是显而易见的。
使用Javascript:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('tr.assoc_row').show();
$('#settings-removed-msg').hide();
$('#new-assoc-msg').hide();
$('#formdeleteassoc').submit(function(e){
e.preventDefault(); //This is working to prevent normal submission of the form.
$.ajax ({
type: 'POST',
url: '<?php echo $cb_t2c_remove_all_url; ?>', //I have checked this to make sure it is the correct url for the PHP file.
data: {removeall: 'yes' //This is the data that is NOT getting passed to the PHP file.
},
success: function() {
$('#settings-removed-msg').fadeIn('fast'); //This gets triggered.
$('tr.assoc_row').fadeOut('fast'); //This gets triggered
}
});
});
});
PHP代码:
<?php
//remove_all.php
global $wpdb;
$prefix = $wpdb->prefix;
$remove_var_dump = $_POST['removeall']; //returning NULL
var_dump($remove_var_dump); //returning NULL
if ( $_POST['removeall'] == 'yes' ) {
echo 'This was set to yes, everything is working.';
}
else {
echo 'This was not set to yes, it is still not working.';
}
?>
解决方案:
/*
JQUERY action processed by AJAX
*/
add_action('init', 'cb_t2c_action_javascript');
function cb_t2c_action_javascript() {
?>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" >
$(document).ready(function(){
$('tr.assoc_row').show();
$('#settings-removed-msg').hide();
$('#new-assoc-msg').hide();
$('#formdeleteassoc').submit(function(e){
e.preventDefault(); //Works to prevent normal submission of the form.
var data = {
action: 'cb_t2c_ajax_action',
removeall: 'yes'
};
// since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
$.ajax ({
type: 'POST',
url: ajaxurl,
data: data,
success: function() {
$('#settings-removed-msg').fadeIn('fast'); //Working now
$('tr.assoc_row').fadeOut('fast'); //Working now
}
});
$('#formsavesettings').submit(function(){
$('#new-assoc-msg').fadeIn('fast'); //Working now
});
});
});
</script>
<?php
}
//Add the action to process the AJAX.
add_action('wp_ajax_cb_t2c_ajax_action', 'cb_t2c_action_callback');
add_action('wp_ajax_nopriv_cb_t2c_ajax_action', 'cb_t2c_action_callback');
function cb_t2c_action_callback() {
global $wpdb; // this is how you get access to the database
$prefix = $wpdb->prefix;
$remove_var_dump = $_POST['removeall']; //returning NULL
var_dump($remove_var_dump);
$removeall = $_POST['removeall'];
if ( isset($removeall) ){
$wpdb->query("DELETE FROM wp_cb_tags2cats");
}
die(); // this is required to return a proper result
}
答案 0 :(得分:1)
尝试设置:
var data = 'removeall=yes';
并将其设置在$ .ajax({})
中data: data,
并看看这是否有效。
答案 1 :(得分:0)
我已修改你的ajax功能,请尝试一下。
jQuery.ajax({
type:"GET",
cache:false,
url:'display_alert_msg.php',
data: 'removeall=yes',
success:function(html){
$('#settings-removed-msg').fadeIn('fast'); //This gets triggered.
$('tr.assoc_row').fadeOut('fast'); //This gets triggered
}
});