JQuery AJAX将空参数传递给我的函数吗?

时间:2019-02-21 作者:Rollor

我正在尝试用ajax做一个简单的表单提交(这样页面就不会刷新)

functions.php

add_action(\'wp_ajax_send_projectmessage\', \'send_projectmessage\');
function send_projectmessage($projectid, $userid, $message) {
    global $wpdb;
    $wpdb->insert(\'tbl_messages\', array(
        \'project_id\' => $projectid,
        \'user_id\' => $userid,
        \'message_body\' => $message
    ));
    echo \'success?\'; //this shows up in console.log but 3 parameters are empty
    wp_die();
}

js:

$(\'#form-pm\').on(\'submit\',function(e) {

    e.preventDefault();

    //hardcode stuff for testing
        var testdata = { 
            \'action\': \'send_projectmessage\',
            \'projectid\': \'71\', 
            \'userid\': \'1\', 
            \'message\': \'voila\' 
        };

        var ajaxRequest =
        $.ajax({
            url: admin_ajax.ajax_url,
            type: \'post\',
            data: testdata
        });

        ajaxRequest.done(function(data) { console.log(data); });
        ajaxRequest.fail(function(jqXHR) { alert(\'You are fail. \' + jqXHR); });      
});
管理ajax。php连接正确。我的错误。日志显示send_projectmessage 正在调用函数,但参数为空。我错过什么了吗?从昨天起我就一直在拉扯头发。

error.log

PHP Warning:  Missing argument 2 for send_projectmessage(), called in /var/www/html/wp-includes/class-wp-hook.php on line 286 and defined in /var/www/html/wp-content/themes/mytheme_child/functions.php on line 178, referer: https://xxxxx
PHP Warning:  Missing argument 3 for send_projectmessage(), called in /var/www/html/wp-includes/class-wp-hook.php on line 286 and defined in /var/www/html/wp-content/themes/mytheme_child/functions.php on line 178, referer: https://xxxxx
WordPress database error Column \'user_id\' cannot be null for query INSERT INTO `tbl_messages` (`project_id`, `user_id`, `message_body`) VALUES (\'\', NULL, NULL) made by do_action(\'wp_ajax_send_projectmessage\'), WP_Hook->do_action, WP_Hook->apply_filters, send_projectmessage, referer: https://xxxxx/

1 个回复
最合适的回答,由SO网友:sMyles 整理而成

这些值不是作为参数传递的,而是在$_POST 大堆

您需要使用check_ajax_nonce https://developer.wordpress.org/reference/functions/check_ajax_referer/

  • 在将提交的值插入数据库之前,需要清理提交的值,具体操作如下:

    add_action( \'wp_ajax_send_projectmessage\', \'send_projectmessage\' );
    
    function send_projectmessage() {
    
        global $wpdb;
    
        check_ajax_referer( \'send_projectmessage\', \'send_projectmessage_nonce\' );
    
        $projectid = sanitize_text_field( $_POST[\'projectid\'] );
        $userid = sanitize_text_field( $_POST[\'userid\'] );
        $message = sanitize_text_field( $_POST[\'message\'] );
    
        $wpdb->insert( \'tbl_messages\', array(
            \'project_id\'   => $projectid,
            \'user_id\'      => $userid,
            \'message_body\' => $message
        ) );
    
        wp_send_json_success();
    }
    
    我也不建议提交userid 通过POST as,用户可以自己定义。如果这是WordPress用户ID,则应使用内部核心函数来获取该值:

    add_action( \'wp_ajax_send_projectmessage\', \'send_projectmessage\' );
    
    function send_projectmessage() {
    
        global $wpdb;
    
        check_ajax_referer( \'send_projectmessage\', \'send_projectmessage_nonce\' );
        $user_id = get_current_user_id();
    
        if ( empty( $user_id ) ) {
            wp_send_json_error( array( \'not_logged_in\' => \'User is not logged in\' ) );
            return;
        }
    
        $projectid = sanitize_text_field( $_POST[\'projectid\'] );
        $message = sanitize_text_field( $_POST[\'message\'] );
    
        $wpdb->insert( \'tbl_messages\', array(
            \'project_id\'   => $projectid,
            \'user_id\'      => $user_id,
            \'message_body\' => $message
        ) );
    
        wp_send_json_success();
    }
    
    有关暂时信息,请参见此处:

    https://codex.wordpress.org/WordPress_Nonceshttps://codex.wordpress.org/Function_Reference/wp_nonce_field

    在页面的某个位置,您需要包含隐藏的nonce字段:

    <?php wp_nonce_field( \'send_projectmessage\', \'send_projectmessage_nonce\' ); ?>
    
    并确保将其包含在帖子中:

    $(\'#form-pm\').on(\'submit\',function(e) {
    
        e.preventDefault();
        var send_projectmessage_nonce = $(\'#send_projectmessage_nonce\').val();
        //hardcode stuff for testing
            var testdata = { 
                \'action\': \'send_projectmessage\',
                \'projectid\': \'71\', 
                \'userid\': \'1\', 
                \'message\': \'voila\',
                \'send_projectmessage_nonce\': send_projectmessage_nonce
            };
    
            var ajaxRequest =
            $.ajax({
                url: admin_ajax.ajax_url,
                type: \'post\',
                data: testdata
            });
    
            ajaxRequest.done(function(data) { console.log(data); });
            ajaxRequest.fail(function(jqXHR) { alert(\'You are fail. \' + jqXHR); });      
    });
    

  • 相关推荐

    如何通过AJAX“加载更多”帖子?

    关于分类法{name}。php模板用于我的“组织”分类法,我目前显示的帖子数量有限,按分类法的每个术语“格式”分组。如果可用帖子的数量超过了可显示的数量,我现在想启用基于AJAX的加载更多帖子。我已经关注了无数关于这个主题的教程和StackExchange问题,但有些东西仍然没有点击。例如,我实现了Misha Rudrastyh\'s \"Load More\" button, 但代码不会加载任何帖子。我迷路了,快要放弃了。我有一个帖子块,上面有这样一个“更多”链接,准备好了,等待着。。。这对应于一个分类