Problem with long Polling

Asked

Viewed 385 times

6

Well I did one here script to fetch information from real-time posts the database but it works if I add it directly in Mysql works well but the page when I go to insert a text does not work with Mysql and disable the script of long Polling it already adds well to database

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>
  • just one remark, the snippet user_id=<? php echo $_SESSION['user_id'] ? > does not need to be sent this way... this features severe security failure.

  • Does this script have to do with the comments you are trying to make? Ajax searching the "posts" is giving problem or what adds?

  • Yes I can not find a solution for it to work as long Polling solves as I want but there are few examples on the net to use with mysql

  • Look @Césarsousa you have no problem with mysql, review the javascript/php part, study it in ajax and json_encode and rewrite your javascript/php code to do it the right way.

  • you’ve solved that issue?

1 answer

2

First part of your code - I made a few adjustments to a 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 adjustments, keep an eye on the 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 you get a type object there in your ajax/process_post.php? Simple I will 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 sources, in what has shown so far is what has p/ adjust.

Browser other questions tagged

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