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!
Executing a PHP routine when clicking a link
Moderator: General Moderators
Re: Executing a PHP routine when clicking a link
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
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.
<?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
Thanks to both of you for your suggestions. It sounds like the php script redirection should work and be pretty simple.