Problem with long polling

6

Well I've done a script here to fetch information from the real-time database, but it works fine if I add it directly to MySQL, but it works fine on the page when I insert text it works for MySQL and if you disable script long polling it already adds the database well

Ajax long polling

<script language="javascript">

var timestamp = null;
function cargar_push() 
{ 
    $.ajax({
    async:  true, 
    type: "POST",
    url: "ajax/funcao.php",
    data: "&timestamp="+timestamp,
    dataType:"html",
    success: function(data)
    {   
        var json           = eval("(" + data + ")");
        timestamp          = json.timestamp;
        opiniao            = json.opiniao;

        if(timestamp == null)
        {

        }
        else
        {
            $.ajax({
            async:  true, 
            type: "POST",
            url: "ajax/mostra_posts.php?id_estabelecimento=<?php echo $row->id; ?>",
            data: "",
            dataType:"html",
            success: function(data)
            {   
                $('#mostra_posts').html(data);
            }
            }); 
        }
        setTimeout('cargar_push()',1000);

    }
    });     
}

$(document).ready(function()
{
    cargar_push();
}); 

</script>

Script that adds post

<script type="text/javascript">
$(function() {
    $(".submit_button").click(function() {
        var textcontent = $("#opiniao").val();
        var dataString = 'id_estabelecimento=<?php echo $row->id; ?>&user_id=<?php echo $_SESSION['user_id'] ?>&opiniao='+ textcontent;
        if(textcontent==''){
            alert("Por favor escreva uma opinião..");
            $("#opiniao").focus();
        }else{
            $("#flash").show();
            $("#flash").fadeIn(400).html('<span class="load">Aguarde por favor..</span>');
            $.ajax({
                type: "POST",
                url: "ajax/processa_post.php",
                data: dataString,
                cache: true,
                success: function(html){
                    $("#show").after(html);
                    document.getElementById('opiniao').value='';
                    $("#flash").hide();
                    $("#opiniao").focus();
                }  
            });
        }
        return false;
    });
});
</script>
    
asked by anonymous 22.02.2015 / 22:51

1 answer

2

First part of your code - I made some adjustments to look at the comments

<?php
    // Assim você escreve na tag HTML que ficará oculta no site
    echo "<input id='id_estabelecimento' type='hidden' value='".$row->id."'>";
    echo "<input id='user_id' type='hidden' value='".$_SESSION['user_id']."'>"; 
?>
<script language="javascript">
var timestamp = null;
function cargar_push(){ 
    $.ajax({
    async:  true, 
    type: "POST",
    url: "ajax/funcao.php",
    data: "&timestamp="+timestamp,
    dataType:"html",
    success: function(data){   
        var json           = eval("(" + data + ")");
        timestamp          = json.timestamp;
        opiniao            = json.opiniao;

        if(timestamp != null){         
            // Aqui você pega o valor da tag oculta no site assim não mistura PHP com javascript
            var id_estabelecimento = $("#id_estabelecimento").val();
            $.ajax({ async:  true, type: "POST", dataType:"html",url: "ajax/mostra_posts.php",
                data: id_estabelecimento,   //esta usando o ajax/POST entao manda o data como POST
                success: function(data){ $('#mostra_posts').html(data); }
            }); 
        }
        setTimeout('cargar_push()',1000);
    }
    });     
}

$(document).ready(function(){ cargar_push(); }); 

</script>

Second part of your code - More settings, keep an eye on your coments

<script type="text/javascript">
$(function() {
    $(".submit_button").click(function() {
        var textcontent = $("#opiniao").val();      
        //var dataString = 'id_estabelecimento=<?php echo $row->id; ?>&user_id=<?php echo $_SESSION['user_id'] ?>&opiniao='+ textcontent;
        //esquece isso vamos usar um tipo objeto para resolver essa parada
        var dataString = new Object();
        dataString.id_estabelecimento = $('#id_estabelecimento').val();
        dataString.user_id = $('#user_id').val();
        dataString.opiniao = textcontent;
        if(textcontent==''){
            alert("Por favor escreva uma opinião..");
            $("#opiniao").focus();
        }else{
            $("#flash").show();
            $("#flash").fadeIn(400).html('<span class="load">Aguarde por favor..</span>');
            $.ajax({
                type: "POST",url: "ajax/processa_post.php",data: dataString,cache: true,
                success: function(html){
                    $("#show").after(html);
                    //document.getElementById('opiniao').value=''; O que é isso opnião vai limpar o campo/form ?
                    $('#opiniao').val(''); // faz o mesmo que a linha de cima, ta usando jQuery entao se acustume
                    $("#flash").hide();
                    $("#opiniao").focus();
                }  
            });
        }
        return false;
    });
});
</script>

How do you get an object type there in your ajax / processa_post.php ? Simple I'll demonstrate

<?php
    $obj = (object) $_POST['dataString']);
    // e agora cade os parametros? abaixo um exemplo de como chama-lós
    $obj->id_estabelecimento;
    $obj->user_id;
    $obj->opiniao;
?>

Making these adjustments, and still not working you will have to go showing more source codes, in what has shown so far that is what you have to adjust.

    
17.04.2015 / 16:16