Jump to content

Executing a PHP routine when clicking a link


mottwsc

Recommended Posts

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!

Link to comment
Share on other sites

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');
});

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.