Pregunta

this is my PHP code_

if(isset($_REQUEST['show']))
{
        $con=mysqli_connect("localhost","root","","server_name");

        $show = mysqli_query($con, "SELECT * FROM name1 ORDER BY date DESC");
        $html='';
        while($showE = mysqli_fetch_array($show)) 
        {
        $html.= '<h3>'.$showE['un_name'].'</h3>';
        $html.= '<div>'.$showE['un_name_dec'].'</div>';
        }
        echo $html;
       //End of while loop

}

this is jquery code_

$.post('preprocessor.php', '&show=', function(data) {
    $("#accordion").html(data);
    $("#accordion").accordion({
       collapsible: true,
       icons: {
            activeHeader: "ui-icon-triangle-1-s",
            header: "ui-icon-triangle-1-e"
        }
    });
    });

this is body_

<div id='accordion'>
</div>  

i am not getting real time updates, everything is working, but the Ajax thing is not working... it works if i refresh or reload the page but not itself

¿Fue útil?

Solución

What you are looking for is setTimeout.

 $("#accordion").accordion({
       collapsible: true,
       icons: {
            activeHeader: "ui-icon-triangle-1-s",
            header: "ui-icon-triangle-1-e"
        }
    });
setInterval(makePostCall, 15000); // decide interval to fetch new updates. 15 sec for example

function makePostCall(){
$.post('preprocessor.php', '&show=', function(data) {
    $("#accordion").html(data);

    });
}

But this is not the proper way of doing this kind of stuff. Consider using Ajax Push Engine

Otros consejos

it works if i refresh or reload the page but not itself

For this you need to use setInterval(); function which will check every specific time period which is given:

   setInterval(function(){
      $.post('preprocessor.php', '&show=', function(data) {
         $("#accordion").html(data);
         $("#accordion").accordion({
                   collapsible: true,
                   icons: {
                   activeHeader: "ui-icon-triangle-1-s",
                   header: "ui-icon-triangle-1-e"
                   }
         });
      });
   },5000); //<---- runs every 5000 ms 

This will run every 5 seconds and will update the #accordion div.

try:

$.post('preprocessor.php', {show:''}, function(data) {
$("#accordion").html(data);
$("#accordion").accordion({
   collapsible: true,
   icons: {
        activeHeader: "ui-icon-triangle-1-s",
        header: "ui-icon-triangle-1-e"
    }
});
},'html');

Are you setting interval to call the code in certain intervals? All you need to do is, on JavaScript add:

 setinterval(function(){/* do the post here */ },1000 /*this is time */);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top