如何使用Javascript和AJAX在PHP文件中获取$\u POST值?
2012年2月28日更新:由于@charlietfl,这个问题在最后有了解决方案 我在一些JQuery脚本中有一个表单和一个AJAX调用,AJAX似乎正在成功执行,但是PHP文件中的$\u POST变量仍然是空的。不知道我做错了什么。下面是我的代码注释 主要问题涉及PHP文件。为什么PHP$\u POST变量没有设置为“是”?如果我进行var_转储,它始终显示NULL。但是,我相信我正在使用典型的AJAX方法手动将“removeall”设置为任意值,在本例中为“yes”。PHP文件不应该将标签为“removeall”的$\u POST变量设置为“yes”吗 我希望无论我做错了什么,都能让别人完全明白 Javascript:如何使用Javascript和AJAX在PHP文件中获取$\u POST值?,php,jquery,ajax,post,Php,Jquery,Ajax,Post,2012年2月28日更新:由于@charlietfl,这个问题在最后有了解决方案 我在一些JQuery脚本中有一个表单和一个AJAX调用,AJAX似乎正在成功执行,但是PHP文件中的$\u POST变量仍然是空的。不知道我做错了什么。下面是我的代码注释 主要问题涉及PHP文件。为什么PHP$\u POST变量没有设置为“是”?如果我进行var_转储,它始终显示NULL。但是,我相信我正在使用典型的AJAX方法手动将“removeall”设置为任意值,在本例中为“yes”。PHP文件不应该将标签为
<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
//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
}
$(文档).ready(函数(){
$('tr.assoc_row').show();
$(“#设置已删除消息”).hide();
$(“#新建关联消息”).hide();
$('#formdeleteassoc')。提交(函数(e){
e、 preventDefault();//这是为了防止表单的正常提交。
$.ajax({
键入:“POST”,
url:“”,//我已检查此url以确保它是PHP文件的正确url。
数据:{removeall:'yes'//这是未传递到PHP文件的数据。
},
成功:函数(){
$('#settings removed msg')。fadeIn('fast');//这会被触发。
$('tr.assoc_row').fadeOut('fast');//这会被触发
}
});
});
});
PHP代码:
<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
//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
}
解决方案:
<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
//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
}
/*
由AJAX处理的JQUERY操作
*/
添加动作('init','cb_t2c_action_javascript');
函数cb_t2c_action_javascript(){
?>
$(文档).ready(函数(){
$('tr.assoc_row').show();
$(“#设置已删除消息”).hide();
$(“#新建关联消息”).hide();
$('#formdeleteassoc')。提交(函数(e){
e、 preventDefault();//用于阻止表单的正常提交。
风险值数据={
动作:“cb_t2c_ajax_动作”,
removeall:“是的”
};
//因为2.8 ajaxurl总是在admin头中定义,并指向admin-ajax.php
$.ajax({
键入:“POST”,
url:ajaxurl,
数据:数据,
成功:函数(){
$(“#设置已删除msg”).fadeIn('fast');//正在工作
$('tr.assoc_row').fadeOut('fast');//正在工作
}
});
$('#formsavesettings')。提交(函数(){
$('new assoc msg').fadeIn('fast');//正在工作
});
});
});
我已经修改了你的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
}
});
尝试设置此选项:
var data = 'removeall=yes';
并在$.ajax({})中设置它
看看这是否奏效。你的数据包嗅探器说了什么?你是如何检查remove_all.php的输出的?Somesh,我实际上是在使用echo以外的东西进行测试,这是对数据库的一个简单操作。例如,if($_POST['removeall']='yes'){$wpdb->query(“从“$prefix.cb_tags2cats删除”)}
如果removeall变量设置为“yes”,那么它应该删除特定表中的所有数据。这与使用向服务器发送的表单有关,但出于其他原因,我需要通过AJAX将$\u POST变量发送到另一个PHP文件。此外,不确定它是否相关,但JQuery不会使用e执行success函数“remove_all.php”或“/remove_all.php”仅适用于作为php变量输入的url。您应该使用wordpress ajax API。如果只包含框架编辑的一部分,您很可能会遇到wordpress安全问题-您可以通过不包含$wpdb引用来检查这一理论,并将帖子转储回以查看内容t返回,这有帮助吗?另外,@Harry_F1也可以,但是他的ajax提交类型设置为“get”,应该更改为“post”以匹配您的后端代码。在我的示例中,我只是尝试抽象值。这肯定是有帮助的,因此+1。Wordpress API构造了与此类似的数据变量。思考abo但是,将数据放入“数据”变量中,让我有了一部分帮助。谢谢。np,我很高兴我能帮上忙:)你还需要其他帮助吗?