更新2/28/2012:由于@charlietfl,这一问题在结尾处有了解决办法。
我在一些JQuery脚本中有一个表单和一个AJAX调用,而且AJAX似乎正在成功执行,但是PHP中的$_POST变量仍然是空的。不知道我做错了什么。下面是我的代码注释。
主要问题涉及PHP文件。为什么PHP变量没有设置为‘是’?如果我做了一个$_POST,它始终显示为NULL。然而,我认为我正在使用典型的var_dump方法手动将'removeall‘设置为任意值,在这个例子中是’是‘。PHP不应该将带有“$_POST”标签的变量设置为“是”吗?
我希望无论我做错了什么,对某人来说都是显而易见的。
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
}发布于 2012-02-28 05:27:38
尝试设置如下:
var data = 'removeall=yes';并将其设置在$.ajax({})中。
data: data,看看这招是否管用。
发布于 2012-02-28 05:24:20
我已经修改了您的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
}
});https://stackoverflow.com/questions/9476755
复制相似问题