Refresh database rows & display without reloading page
I need to update database rows and display the change without reloading the page.
This is what I use currently to display the information:
<?php
$limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;
$query = "SELECT * FROM craffyposts ORDER by time DESC $limit";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_assoc($result)) {
echo $row['like']; // This is what needs to be changed when database is updated.
?>
<a href="like.php?id=<?php echo $row['id']; ?>" title="Like"></a>
<?php
};
?>
like.php:
<?php
$cid = $_GET['id'];
database_connect();
$query2 = "SELECT * FROM craffyposts WHERE id = '".$cid."'";
$result2 = mysql_query($query2) or die(mysql_error());
while ($row2 = mysql_fetch_assoc($result2)) {
$lk = $row2['like'];
};
$nlk = $lk +开发者_Go百科 "1";
mysql_query("UPDATE craffyposts SET `like` = '".$nlk."' WHERE id = '".$cid."'") or die(mysql_error());
echo "<script type='text/javascript'>window.location='index.php';</script>";
?>
Any help?
Without reloading the page? You'll have to use a client-side request to fetch new data from the server. the most common of such would be JavaScript's AJAX. You can use AJAX to fetch data from the server (presumably in some quickly parsed format such as JSON) and then load the data into the page with JavaScript.
use JavaScript (or a JS Framework, I prefer jQuery) and use Ajax-Methods to display the data.
精彩评论