How to save request data in sessionStorage or Cookies?

0

I am developing a chat with WebSocket and there is a list of users, when I click on a certain user, a conversation starts with only that user, my doubt is this: clicking the user to start a new conversation is I made a request that returns all my conversations with that user (using ajax ()), if I click again on the list to start a new conversation with that same user, it again requests the messages. I would like to save the data returned in a Cookie or HTML5 sessionStorage so that if I click again on this user to start a conversation I will not need to reorder.

ps: When you click on a user to start a conversation, it adds an html () with the messages returned via ajax () from a PHP file in the div where the messages are. If I clicked to start a conversation with another user I would like the procedure to be the same.

jQuery code:

function add_janelas(id, nome){
    var nome_add = '<p class="nome-informacoes '+id+'" id="'+id+'">'+nome+'</p>';
    var content_msg = '<div id="jan_'+id+'" class="janela_conv '+id+'"></div>';
    $('.informacoes').html(nome_add);
    $('.message_box').html(content_msg);
}

$(document).ready(function(){
    var janelas = new Array();
    var users = new Array();
    var comecar = $(".comecar");

comecar.click(function(){

    users = [];
    var id = $(this).attr('id');
    var nome = $(this).attr('title');

    add_janelas(id, nome);

    $.ajax({
        type: 'POST',
        url: 'sys/chat.php',
        data: 'acao='+'atualizar'+'&id='+id,
        success: function(html){
            $(".janela_conv").html(html);
        }
    });

    });
});

PHP code (chat.php):

<?php
    session_start();
    include_once '../config.php';
    require_once ("../classes/BD.class.php");
    BD::conn();

    date_default_timezone_set('America/Sao_Paulo');

    $acao = $_POST['acao'];

    switch ($acao) {    
        case 'atualizar':
            $id = isset($_POST['id']) ? $_POST['id'] : '';

            $select = BD::conn()->prepare("SELECT * FROM mensagem WHERE id_de = ? AND id_para = ? OR id_de = ? AND id_para = ?");
            $select->execute(array($_SESSION['id_user'], $id, $id, $_SESSION['id_user']));

            $mensagem = '';
            while($fetch = $select->fetchObject()){
                $nome = BD::conn()->prepare("SELECT nome FROM usuarios WHERE id = ?");
                $nome->execute(array($fetch->id_de));
                $remetente = $nome->fetchObject();

                $mensagem .= "<div class='mensagem'><span class='user_name' nome=" . $remetente->nome . " style=''>" . $remetente->nome . "</span> : <pre><span class='user_messag'>" . $fetch->msg . "</span></pre></div>";

                echo $mensagem;
            }
        break;
    }
?>

    
asked by anonymous 22.04.2015 / 16:18

1 answer

1

You will have to write to localStorage the value that it receives when it opens the first time, when there is another click, you will check if the localStorage of this window already exists, if yes show that html instead of the html that comes from php:

comecar.click(function(){

    users = [];
    var id = $(this).attr('id');
    var nome = $(this).attr('title');
    var locStorage = window.localStorage;
    var hasConvo = locStorage.getItem(nome);
    add_janelas(id, nome);
    if (!hasConvo) {
        $.ajax({
            type: 'POST',
            url: 'sys/chat.php',
            data: 'acao='+'atualizar'+'&id='+id,
            success: function(html){
                locStorage.setItem(nome,html);
                $(".janela_conv").html(html);
            }
        });
    }
    else {
        $('.janela_conv').html(hasConvo);
    }

});
    
22.04.2015 / 16:37