How do I send a message without having to refresh the page?

Asked

Viewed 28 times

0

If you have any questions, I’m available.

//obtem as informações do banco de dado com o methodo json_encode() php consulta.php
    //=====================================================
    function atualizarItems(){
            var items = "";
            $.getJSON('consulta.php', function(resultado){
                for(var i = 0; i < resultado.length; i++){
                    items += '<div>';
                    items += '<img class="avatarUser" src = "avatar/'+resultado[i].avatar+'" title="'+resultado[i].username+'">';
                    items += '<p class="nomeUser">' + resultado[i].username + '<p>';
                    items += '<p class="msgUser">' + resultado[i].mensagem + '<p>';
                    items += '<a href="delete.php?id='+resultado[i].id_post+'">delete</a>';
                    items += '</div>';
                }
                $('.comenUser').html(items);
            })
            //$.getJSON('consulta.php')
        }//<<<< tudo OK ate aqui
//======================================================
$(function () {
    $('#form_post').submit(function (event) {
        event.preventDefault(); //tira o efeito padrão

    var url = 'add_new_post.php'; //pagina do php para enviar os dado
    var el = $('#form_post').serialize(); //AQUI NEM PRECISO FALA    

    //aviso de erro
    var fail = '<p class="feed" ><i class="fa fa-angle-double-down fa-fw"></i> Erro..</p>';
    //aviso de nova mensagem
    var reload = '<p class="feed" ><i class="fa fa-angle-double-down fa-fw"></i> nova mensagem</p>';

    /*
    aqui e para quando clickar na nova mensagem a informações do usuario
    aparece no campo de comentario e a informaçao de nova mensagem desaparece...

    */
    var refresh = $('.feed');

        $.post(url, el, function () {
            $('.feed').html(reload).fadeIn(500);
            refresh.on('click', function (e) {
                e.preventDefault();
                $('#text_mensagem').val("");
                $('.feed').remove().fadeOut(500);
                atualizarItems();
            })
        })
    })
}())
//ate aqui esta tudo certo
/**
 * O problema é que quero pode enviar mais novas mensagem
 * tipo um loop quando vez eu quizer
 * sem precisa atualizar a pagina toda
 * /
  • Yeah, but what’s the matter, young man?

  • the problem is that for the reason I gave to taking out the standard event of Submit I can not send again a new message to keep the circle,

  • Do you want to send the message with another event? For example by pressing enter key?

  • it’s like I send the message to the data bank and then ajax makes a request telling me that a new message I click on this information and then the message appears, then I send another message so that when I do this nothing happens, i have to update the whole page it seems that preventDefault makes the form Submit only works once

  • try this and see if it solves your problem: $(document).on('submit','#form_post', function (event) { ... } and $(document).on('click', '.feed',function (e) { ... }

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.