【问题标题】:How do I get a $_POST value in a PHP file using Javascript and AJAX?如何使用 Javascript 和 AJAX 在 PHP 文件中获取 $_POST 值?
【发布时间】:2012-02-29 06:55:44
【问题描述】:

2012 年 2 月 28 日更新:感谢@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
}

【问题讨论】:

  • 你的数据包嗅探器说什么?
  • 你如何检查 remove_all.php 的输出?
  • Somesh,我实际上是在使用 echo 以外的东西进行测试,这是对数据库的简单操作。例如,if ( $_POST['removeall'] == 'yes' ){ $wpdb-&gt;query("DELETE FROM ".$prefix."cb_tags2cats"); } 如果 removeall 变量设置为“是”,那么它应该从特定表中删除所有数据。这适用于使用发布到服务器的表单,但出于其他原因,我需要通过 AJAX 将 $_POST 变量发送到另一个 PHP 文件。
  • 另外,不确定它是否相关,但 JQuery 不会使用“remove_all.php”或“/remove_all.php”执行成功函数。它仅适用于作为 PHP 变量输入的 url。
  • 您应该使用 wordpress ajax API。通过仅包含部分框架 EDIT,您很可能会遇到 wordpress 安全问题 - 您可以通过不包含 $wpdb 引用来检查此理论,然后将 POST 转储回去以查看返回的内容

标签: php jquery ajax post


【解决方案1】:

尝试设置:

var data = 'removeall=yes';

并在您的 $.ajax({}) 中设置它

data: data,

看看这能不能解决问题。

【讨论】:

  • 这有帮助吗?同样,@Harry_F1 也可以,但他的 ajax 提交类型设置为“get”,应更改为“post”以匹配您的后端代码。在我的示例中,我只是尝试抽象值。
  • 这绝对有帮助,因此 +1。 Wordpress API 的数据变量与此类似。考虑将数据放入“数据”变量中让我成为了其中的一部分。欣赏它。
  • np,我很高兴能帮上忙 :) 你还需要其他帮助吗?
【解决方案2】:

我已经修改了你的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
        }
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多