WordPress AJAX表单出现问题

时间:2020-03-31 作者:Edegist

我正在尝试使用ajax将表单中的数据保存到用户元,但没有成功。

我对jQuery还很陌生,不确定我会错在哪里,因为似乎没有任何suntax错误。

以下是我的插件文件中的代码:

/**
     * Action hooks
     */
        // Register and Enqueue plugin styles and scripts
        add_action( \'wp_enqueue_scripts\',\'load_css_and_js\');
        function load_css_and_js() {
            wp_register_style( \'krv-style\', plugin_dir_url( __FILE__ ) .\'css/custom-setts.css\' );
            wp_enqueue_style(\'krv-style\');
            wp_register_script( \'krv-script\', plugins_url( \'js/custom-setts.js\', __FILE__ ), array(\'jquery\'), null, false );
            wp_enqueue_script(\'krv-script\');
            wp_localize_script( \'krv-script\', \'krv_obj\', array( \'ajax_url\' => admin_url(\'admin-ajax.php\'), \'check_nonce\' => wp_create_nonce(\'krv-nonce\') ) );
        }

        function aj_save_setts_settings( $user_id ) {
            check_ajax_referer( \'krv-nonce\', \'security\' );
            $krv_user_setts = $_POST[\'user_setts\']; 

            if ( !current_user_can( \'edit_user\', $user_id ) ) { return false; }
            update_usermeta( $user_id, \'user_setts\', $krv_user_setts );
        }

        //Hook Function into WP_Ajax for Admin and Frontend
        add_action(\'wp_ajax_aj_save_setts_settings\', \'aj_save_setts_settings\');
        add_action(\'wp_ajax_nopriv_aj_save_setts_settings\', \'aj_save_setts_settings\');

        //Add [setts] Form Shortcode
        add_shortcode( \'setts\', \'add_user_setts_shortcode\' );
        function add_user_setts_shortcode(){
            ?>
            <!-- Create setts Checkbox Loop -->
            <div class="container">
                <form name="myform" id="myform" method="POST">
                    <ul class="ks-cboxtags">
                        <!-- (Loop) -->
                        <li><input name="user_setts[]" id="<?php echo $setts->term_id; ?>" value="<?php echo $setts->term_id; ?>" <?php echo $selected; ?> type="checkbox"/><label for="<?php echo $setts->term_id; ?>"><?php echo $setts->name; ?></label></li>
                    </ul>
                    <input name="myBtn" class="kvo_ajax_submit_button" type="submit" value="Save Settings">
                </form>
            </div>
            <?php
        }
?>
这就是我所拥有的js/custom-setts.js:

jQuery(document).ready( function() {
    jQuery(\'.kvo_ajax_submit_button\').click(function(){

        var user_setts = user_setts;
        var str = {
                \'action\': \'aj_save_setts_settings\',
                \'security\': krv_obj.check_nonce,
                \'user_setts\': user_setts,
            };
        jQuery.ajax({
                type: "POST",
                dataType: "html",
                url: krv_obj.ajax_url,
                data: str,
                success: function(data){
                    // Code after ajax success

                },
                error : function(jqXHR, textStatus, errorThrown) {
                    $loader.html(jqXHR + " :: " + textStatus + " :: " + errorThrown);
                }
        });
    });

});
非常感谢您的帮助!

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

好的,经过长时间的讨论,Edegist和我实际上把这一切都解决了。以下是构成解决方案的几个关键因素。

首先,我们必须循环通过所有选中的复选框才能通过AJAX。

jQuery( document ).ready( function( $ ) {
    $( \'.kvo_ajax_submit_button\' ).click( function( e ) {
        e.preventDefault();
        var user_categories = $( \'input[name="user_categories\\\\[\\\\]"]\' ).map( function() {
            if( $( this ).is( \':checked\' ) ) {
                return $( this ).val();
            }
        } ).get();
        var str = {
            \'action\': \'aj_save_category_settings\',
            \'security\': krv_obj.check_nonce,
            \'user_categories\': user_categories,
        };
        $.ajax({ 
            type: \'POST\',
            dataType: \'html\',
            url: krv_obj.ajax_url,
            data: str,
            success: function( data ) {
                // Code after ajax success
                console.log("Success");
            },
            error : function(req, err){
                console.log(\'Error:\' + err);
            }
        } );
    } );
} );
下一步是找出它为什么没有保存到user\\u元数据库。我们意识到AJAX没有传输$user\\u id,所以我们解决了这个问题。

         function aj_save_category_settings( $user_id ) {
          $user_id = get_current_user_id();
            check_ajax_referer( \'krv-nonce\', \'security\' );
            $krv_user_categories = $_POST[\'user_categories\'];
            if ( !current_user_can( \'edit_user\', $user_id ) ) { return false; }
            update_user_meta( $user_id, \'user_categories\', $krv_user_categories, false );
            wp_die(); // this is required to terminate immediately and return a proper response
        }

相关推荐

PHP致命错误:未捕获错误:在新的WordPress安装中调用未定义的函数wp_kses_Normize_Entities()

我正在尝试在NetBSD(unix)上安装Wordpress 5.3.2。phpinfo()告诉我正在运行Apache/2.4.33(Unix)PHP/7.2.6/wp管理/设置配置。php?步骤=1在窗口底部(在“提交”按钮下方)有一条消息,表示“您的网站出现严重错误”。考虑将WP\\u DEBUG设置为“true”,希望得到错误消息,我手动配置了WP config。php,但正在安装。php我得到一个空白屏幕。服务器日志显示“调用未定义函数wp\\u kses\\u normalize\\u enti