建立一个基于AJAX的wordpress插件

时间:2019-03-18 02:39:01

标签: ajax wordpress messenger

    public function intelFrontendChat(){
        ob_start();
        ?>
        <html>
        <html lang="en">
        <form id="frmmain" name="frmmain" method="post">
        <head>
            <meta charset="utf-9">
            <h5> Unit Briefing </h5>
        </head>
        <body>
        <?php
        global $current_user;
        get_currentuserinfo();
        $user = wp_get_current_user();
        $role = ucwords(str_replace("_"," ",$user->roles[0]));
        $user_id_for_post = $current_user->display_name;
        ?>
            <div id="div_user_name" class="div_user_class">
                <input name="user_id_message" id="user_id_message" type="hidden" value="<?php echo $user_id_for_post?>, <?php echo $role?>" />
            </div>
            <div id="div_chat_display" class="chat_display"></div>
            <div class = "submition_for_chat_int" id="div_send">
            <p>
                <input type="text" id="txt_message_id" name="txt_message" style="width: 100%" /><br></p>
                <p><input type="button" name="btn_send_chat" id="btn_send_chat" value="Send" /></p>
            </div>
        </form>
        </body>
        </html>

        <?php
    }
    public function asIntelChatTable(){
        //This is the table for mission briefings and general chat
        global $wpdb;
        $table = $wpdb->prefix . "intel_chat_table"; 
        $charset_collate = $wpdb->get_charset_collate();
        $sql = "CREATE TABLE $table (
                `id` mediumint(9) NOT NULL AUTO_INCREMENT,
                `userid` varchar(255),
                `message` varchar(255),
             PRIMARY KEY  (`id`)
        ) $charset_collate;";
        require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
        dbDelta( $sql );
    }
    public function intelChatRoomCallBack(){
        global $wpdb;
            $table_name = $wpdb->prefix . "intel_chat_table";
            $ChatsOne = $_POST['chat_insert_data'];
            $ChatsOneUser = $_POST['chat_insert_user'];
            $rows_affected = $wpdb->insert( $table_name, array( 
                //'id' => 1,
                'userid' => $ChatsOneUser,
                'message' => $ChatsOne
            ));
            die();
    }
    public function aSCustomDataDisplayChatFromTable(){
            global $wpdb;
            $table_name = $wpdb->prefix."intel_chat_table" ;
            $chatretrieve_datas = $wpdb->get_results( "SELECT * FROM $table_name" );
            foreach ($chatretrieve_datas as $chatretrieve_data){
            $intel_chat_data = array(
            $chatretrieve_data->userid, ': ',
            $chatretrieve_data->message, '<br/>'
            );
            }
            wp_send_json($intel_chat_data);
            die();
        }
}

这是我的JavaScript

var formId = document.getElementById("frmmain");
if (formId){
    var autoRefreshMessages = function(){
        jQuery.ajax({ 
            url : intel_chat_local_script.ajaxurl,
            type : 'post',
            data : {
                action : 'display_message_in_table',
                currentChat : 'intel_chat_data',
                },          
            success : function(data) {
                console.log(data);
                jQuery("#div_chat_display").append(data);
                }
        });     
    //setTimeout(autoRefreshMessages, 3000);
    };
    autoRefreshMessages();
    btnSendChat.addEventListener("click", function(){
        var fieldTxtInput = document.querySelector('.submition_for_chat_int [name="txt_message"]').value
        var userTxtInput = document.querySelector('.div_user_class [name="user_id_message"]').value
        jQuery.ajax({
            url : intel_chat_local_script.ajaxurl,
            type : 'post',
            data : {
                action : 'insert_message_in_table',
                chat_insert_data : fieldTxtInput,
                chat_insert_user : userTxtInput
                },
            success : function() {
                document.getElementById("txt_message_id").value = '';
                document.getElementById("btn_send_chat").reset;
                }
        });

    });
};
});

我正在尝试构建消息传递系统wordpress插件。但是,我真的在构建自动刷新消息功能方面苦苦挣扎,该功能将列出表中的所有消息。

我为wp_send_json设置了一个函数,并为自动刷新设置了javascript函数,但是它仅显示最新的表条目。我还尝试设置两个相反的变量,以便如果它们彼此不相等来更新表,但是我也没有运气。

0 个答案:

没有答案