mottwsc Posted August 23, 2011 Share Posted August 23, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/245468-executing-a-php-routine-when-clicking-a-link/ Share on other sites More sharing options...
doddsey_65 Posted August 23, 2011 Share Posted August 23, 2011 when the user clicks a link, the browser is going to go there. There is no way to halt this in php so you can run another script/process first. The only way to do this would be to halt the process with javascript/jquery, run your code, and then continue to the page in question: This is the jquery way: $('a.myLink').click(function(e){ e.preventDefault(); //run your code here // then continue to go to the page document.location = $(this).attr('href'); }); Quote Link to comment https://forums.phpfreaks.com/topic/245468-executing-a-php-routine-when-clicking-a-link/#findComment-1260765 Share on other sites More sharing options...
Nodral Posted August 23, 2011 Share Posted August 23, 2011 why not include the PHP in the top of the called page prior to outputting the headers? Quote Link to comment https://forums.phpfreaks.com/topic/245468-executing-a-php-routine-when-clicking-a-link/#findComment-1260870 Share on other sites More sharing options...
mottwsc Posted August 23, 2011 Author Share Posted August 23, 2011 doddsey_65, thanks for the suggestion - I'll look into that. Nodral - the problem is that, depending on which of several links is clicked, the code that has to be executed is going to be slightly different, so I can't run one set of code before the html renders. I need to (a) pass a session variable to another server (if needed, depending on the server I'm coming from and going to), and (b) incorporate the right path information in the link. Quote Link to comment https://forums.phpfreaks.com/topic/245468-executing-a-php-routine-when-clicking-a-link/#findComment-1260906 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.