Get data from PHP using Ajax

Hi,
I am trying to display PHP data with Ajax but I’m missing sth…
Here is my PHP code:

<?php                            
include 'connect.php';						
$stmt = $dbh->prepare("SELECT * FROM testimonials ORDER BY RAND() LIMIT 0,1");
$stmt->execute();

while ($row = $stmt->fetch()){
     echo "<p>". $row['testimonial'] ."</p>";
}                            
?>

and then some Ajax:

<style>
	function get_fb(){
        var feedback = $.ajax({
	type: "POST",
	url: "testimonials.php",
	async: false
	}).success(function(){
	setTimeout(function(){get_fb();}, 5000);
	}).responseText;
								
	$('div.feedback-box').html(feedback);
	}	
	</style>
	<div class="feedback-box"></div>

I wanr to display new content from my database, every 5 seconds.

Thanks a lot!

Hi,

You have the script within <style> tags. That’ll need sorting.

Code-wise, something like this would be better:

function get_fb() {
  $.ajax({
    type: "POST",
    url: "testimonials.php",
  }).success(function(res) {
    $('div.feedback-box').html(res);
  })
}

setInterval(get_fb, 5000);

This is untested, but hopefully you get the idea.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.