Zitate » | Java Scripts » | Flash » | DHTML » | Design Links »
 

Javascript JS & PHP

How to run PHP using the Javascript sentence OnClick

To implement I'll use a click counter:

The problem: can be defined as “How implement a counter of visitors of links with Onclick” or “how count clicks using PHP and JS Onclick”. At that point, I was customizing a Directory script and I wanted to count every visit of each link using PHP and a MySQL database. Basically the database has the link’s URL and a counter. Most of the javacript and PHP tutorials said you can not do this, so… how to count clicks using PHP?…

The solution:

First I want that “on clicks” the link open in a blank window:

<A HREF="PAGE_TO_VISIT.html]" target="_blank">PAG TITLE</A>

Then the Java jscript’s : OnClick is used to execute a PHP script embedded in regular PHP page:

<A HREF="PAGE_TO_VISIT.html" OnClick="parent.location='clickcounter.php?url=PAGE_TO_VISIT.html'" target="_blank">PAGE TITLE</A>

-The Javascript OnClick uses the parent.location to open clickcounter.php page in a parent windows, so, the current page updates the counter in the same window and the link will open in a separate window.

-I pass the parameter "URL" to locate the MySQL specific record and increment the counter in 1: clickcounter.php?url=PAGE_TO_VISIT.html

The clickcounter.php code:

<?
// get the URL from the variable
$urlupdate = $_GET['url'];
// updates the counter
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password);
mysql_select_db($mysql_database, $bd);
$update = "UPDATE pages SET accesscounter = accesscounter + 1 WHERE url = '$urlupdate'";
$result = mysql_query($update);

//return to previous screen
echo '<script type=""text/javascript"">history.go(-1);</script>';
?>

and Voilá, I had executed PHP using Javascript JS OnClick

Credits: Scripts created and maintained by Sergio Vargas-Sanabria, please use contact information on the header. http://www.peoplecnc.com/java_php.html

PAUL E. HARRISON'S NOTEBOOK  

Quick Links:


 
Design: Paul E. Harrison © 2007 www.thefatharry.de | Startseite | The Fat Harry | Design | Termine | Kontakt |