02-14-2008, 10:30 AM
|
#15 (permalink)
|
|
The Addict
Join Date: Nov 2007
Location: UK
Posts: 319
Thanks: 18
|
You can make it update the page instantly without having to refresh using something like this. You create 2 pages on with that script ReSpawn said or whatever and then the main page where you have some kind of onClick or something.
PHP Code:
<script type="text/javascript"> //<![CDATA[ var request = false;
if(window.XMLHttpRequest){ request = new XMLHttpRequest(); }else if(window.ActiveXObject){ request = new ActiveXObject("Microsoft.XMLHTTP"); }
function getData(source){ if(request){
var sourceId = document.getElementById("hiddenFieldId").value; var newsource = source + "?id=" + sourceId; request.open("GET",newsource); request.onreadystatechange = function(){ if(request.readyState < 4){ document.getElementById("totalviews").innerHTML = "Updating...."; } else if(request.readyState==4){ document.getElementById("totalviews").innerHTML = request.responseText; } } request.send(null); } }
//]]> </script>
To use the script you can use something like
PHP Code:
<a href="#" onClick="getSource('process_views.php');"><div id="totalviews"> 4 Views </div></a>
Or something along that line anyway.
This example has a use of a hidden field containing the id then you can use the MySQL on a new page.
PHP Code:
$postQuery = mysql_query('SELECT * FROM post WHERE id = "'.$_GET['id'].'"'); $postFetch = mysql_fetch_array($postQuery);
$newViews = $postFetch['viewcount'] + 1;
mysql_query('UPDATE post SET viewcount = "'.$newViews.'" WHERE id = "'.$_GET['id'].'"');
echo $numViews; // echo the new number of views to be displayed
|
|
|