jeudi 22 janvier 2015

Fetch random row from MySQL on click

I am creating a random quote generator and store the data in a MySQL database. The code is working but it continues to fetch the same row and not a new random row everytime I click. But if I wait for say 30 seconds and click again, it works as it should. I just want it to work right away on click.



<script type="text/javascript">
$(document).ready(function() {

$(".display").click(function() {

$.ajax({ //create an ajax request to load_page.php
type: "GET",
url: "display.php",
dataType: "html", //expect html to be returned
success: function(response){
$("#responsecontainer").html(response);
setTimeout(response, 5);
//alert(response);
}

});
});
});


I suspect the timeout function is not working properly as it takes longer to fetch a new row. I used the order by Rand() to get the random row. My PHP looks like this:



<?php
include("connect.php");
$finds = "SELECT * FROM citater5 ORDER BY RAND() LIMIT 1";
$result = mysql_query($finds, $con) or trigger_error(mysql_error()." ".$finds);

while($data = mysql_fetch_row($result))
{
echo "<aside class=\"citatout\">";
echo "<div id=\"paradiso\" class=\"text-vertical-center-q\">";
echo utf8_encode("<h1 class=\"animated fadeIn\" align=center>$data[0]</h1>");
echo utf8_encode("<h2 class=\"animated fadeIn\" align=center>$data[1]</h2>");
echo "</div>";
echo "</aside>";
}

?>


Why is a new row not being fetched everytime?





Aucun commentaire:

Enregistrer un commentaire