Executing a PHP routine when clicking a link

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mottwsc
Forum Commoner
Posts: 55
Joined: Sun Dec 23, 2007 8:01 pm

Executing a PHP routine when clicking a link

Post 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!
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Executing a PHP routine when clicking a link

Post 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.
greip
Forum Commoner
Posts: 39
Joined: Tue Aug 23, 2011 8:23 am
Location: Oslo, Norway

Re: Executing a PHP routine when clicking a link

Post 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.
mottwsc
Forum Commoner
Posts: 55
Joined: Sun Dec 23, 2007 8:01 pm

Re: Executing a PHP routine when clicking a link

Post by mottwsc »

Thanks to both of you for your suggestions. It sounds like the php script redirection should work and be pretty simple.
Post Reply