[php] jQuery - Call ajax every 10 seconds

I have a mysql feedback database constructed like this:

name | location | feedback

Ryan | England | great support

Obviously there's more entries than that. I am trying to build a feedback div, where it displays a new feedback item every 10 seconds via ajax.

So I have constructed this:

$(document).ready(function(){
   new get_fb(); 
 });

function get_fb(){
var feedback = $.ajax({//Ajax
                        type: "POST",
                        url: "feedback.php",
                        async: false
                        }).responseText;//end of ajax

$('div.feedback-box').html(feedback).delay(10000).queue(function() {
    new get_fb(); 
    });
}

And here's my PHP file:

$result = mysql_query("SELECT * FROM feedback ORDER BY RAND() LIMIT 0,1");
while($row = mysql_fetch_array($result))
{
    $name = $row['name'];
    $location = $row['location'];
    $feedback = $row['feedback'];

    echo "
    <p>Name: $name, Location: $location, Feedback: $feedback.</p>
    ";
} 

However, this only shows two. It doesn't keep showing new ones, it purely shows the first then the second and stops.

What am I doing wrong? Thanks :)

This question is related to php jquery ajax

The answer is


setInterval(function()
{ 
    $.ajax({
      type:"post",
      url:"myurl.html",
      datatype:"html",
      success:function(data)
      {
          //do something with response data
      }
    });
}, 10000);//time in milliseconds 

You could try setInterval() instead:

var i = setInterval(function(){
   //Call ajax here
},10000)

This worked for me

setInterval(ajax_query, 10000);

function ajax_query(){
   //Call ajax here
}

Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to jquery

How to make a variable accessible outside a function? Jquery assiging class to th in a table Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Getting all files in directory with ajax Bootstrap 4 multiselect dropdown Cross-Origin Read Blocking (CORB) bootstrap 4 file input doesn't show the file name Jquery AJAX: No 'Access-Control-Allow-Origin' header is present on the requested resource how to remove json object key and value.?

Examples related to ajax

Getting all files in directory with ajax Cross-Origin Read Blocking (CORB) Jquery AJAX: No 'Access-Control-Allow-Origin' header is present on the requested resource Fetch API request timeout? How do I post form data with fetch api? Ajax LARAVEL 419 POST error Laravel 5.5 ajax call 419 (unknown status) How to allow CORS in react.js? Angular 2: How to access an HTTP response body? How to post a file from a form with Axios