Page 1 of 1

Executing a PHP routine when clicking a link

Posted: Mon Aug 22, 2011 9:16 pm
by mottwsc
I have a PHP application where I am trying to execute a PHP routine when the user clicks on a link to go to another page. The routine needs to execute before I use a header location command to send the user to the next location. I can't do this all with JavaScript, but it could be part of the solution if necessary. I'd prefer to just use PHP if I could, but I'm not sure that this is possible.

Can anyone share with me a brief example of how I could do this?

Thanks!

Re: Executing a PHP routine when clicking a link

Posted: Tue Aug 23, 2011 8:42 am
by Celauran
Without much in the way of details, I'd initially look at an AJAX solution. Have a JS function triggered onclick, which then sends an XMLHttpRequest to some PHP script.

Re: Executing a PHP routine when clicking a link

Posted: Tue Aug 23, 2011 8:45 am
by greip
What you are looking for is a simple PHP-script like this:

<?php

/* Do whatever you want the script to do */

header ( "Location: http://example.com/" ); /* redirect the user to the final web address */

?>

The link the user clicks on should lead to this script. The script will redirect the user onwards to the final destination.

Re: Executing a PHP routine when clicking a link

Posted: Tue Aug 23, 2011 1:17 pm
by mottwsc
Thanks to both of you for your suggestions. It sounds like the php script redirection should work and be pretty simple.