Javascript JS and PHP How to run PHP using Java OnClick

 

Javascript JS & PHP How to run PHP using the Javascript sentence OnClick

Versión en español: Javascript JS y PHP Como ejecutar código PHP usando la función OnClick de JavascriptLunarpages hosting php jsp asp perl 3000 in HD 40 Gigabytes data transfer 999 email accounts

VALUABLE HELP FOR WEBMASTERS

Caveat: Is it possible to call PHP script using Javascript JS? It is a common question of Webmasters implementing complex coded web applications. The answer is NO*, however you can do it using this trick!. The answer is still No, because Javascript is a piece of code at the browser, client-side application and PHP is at the server-side, different technologies, different applications that cannot beOracle E1 Peoplesoft CNC Technical Support for EnterpriseOne mixed, however there is a possibility.

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 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.

See it in action with my Live Demo, notice this my sandbox so sometimes it does not work because I´m playing around. The first link has the counter, complementary results are provided by my script viXML, so you will not see the counter on it.

Credits: Scripts created and maintained by Sergio Vargas-Sanabria, please use contact information on the header.

 


"Execute PHP using Javascript JS OnClick" - Comments and Questions:

Comments by bigarma - none - August 8, 2005, 10:48 pm
Worked beautifully - well done! :smile:
Comments by Truantduck - none - November 23, 2005, 2:12 pm
Looks like the author of this article doesn't know the diffrence between web page javascript and java. They are not the same. Not even close.
Comments by marc - none - July 13, 2006, 4:52 am
so basically there is no way for me to log user activity in web pages?
Comments by aleks - none - August 13, 2006, 7:17 pm
:smile: It is very good trick. I struggeled a little bit with php print ".. onclick ..." to put inside onclick and thi did not work. when I put onclick outside the print it works beautifly.
Thanks
http://www.exploreluxury.com
Comments by Dave - none - January 15, 2007, 11:09 pm
This technique worked great the first time! - THANKS MUCH :smile:
I have no idea why someone would mention Java in a comment; near as I can make out, this has nothing to do with Java
Comments by Sergio The Author - none - February 7, 2007, 8:14 am
That happened because I wrote Java instead of JS..ooops. I fixed later the article
Comments by Adrian - L33_adrian@yahoo.com - October 21, 2007, 6:47 pm
YES, it is possible using AJAX.
Comments by Kapil - tandon@activist.com - December 10, 2007, 8:13 pm
:tongue: See this Code:

alert('LT ?php echo $_GET['t'] ?GT')

or phpinfo( );

Javascript calling php on a PHP page is possible.Is'nt it.

Java & PHP How to run PHP using Java OnClick iconUse this space to comment PHP and Java OnClick
Do you have any idea how improve this script? let us know...
21 May 2005


MORE WEBMASTERS VALUABLE HELPS AND TOOLS:

viXML Parse Web Search results in your web pages using Gigablast Metasearch XML Services.

READ_COMMENTS for CutePHP NEWS: script to parse comments for specific topic - news


Más scripts y programas gratis para WebMasters:

MOD_REWRITE ayuda. Como convertir páginas dinámicas en estáticas

Ejecutar código PHP in páginas .HTML Como crear páginas HTML que corran código PHP

Lunarpages hosting php jsp asp perl 3000 in HD 40 Gigabytes data transfer 999 email accounts

 

|          Contact         |

Request a Quote     | CNC Qualifications         | Links and Resources

 Sergio Vargas-Sanabria © 2005

PEOPLECNC.COM belongs to VLialin Inc.US based company. All Rights Reserved, Java & PHP How to run PHP using Javascript JS OnClick