I want to have a page run some PHP code when a user clicks on a link, without redirecting them. Is this possible with
<a href=""></a>
or with the javascript onclick event?
I want to have a page run some PHP code when a user clicks on a link, without redirecting them. Is this possible with
<a href=""></a>
or with the javascript onclick event?
You cant run PHP when a user clicks on a link without leaving the page unless you use AJAX. PHP is a serverside scripting language, meaning the second that the browser sees the page, there is no PHP in it.
Unlike Javascript, PHP is ran completely on the server, and browser wouldn't know how to interpret it if it bit them on the rear. The only way to invoke PHP code is to make a Page request, by either refreshing the page, or using javascript to go fetch a page.
In an AJAX Solution, basically the page uses javascript to send a page request to another page on your domain. Javascript then gets whatever you decide to
echo
in the response, and it can parse it and do what it wants from there. When you are creating the response, you can also do any backend stuff like updating databases.As others have suggested, use JavaScript to make an AJAX call.
http://docs.jquery.com/Ajax/jQuery.ajax
This should work as well
Thanks,
cowtipper
I know this post is old but I just wanted to add my answer!
You said to log a user out WITHOUT directing... this method DOES redirect but it returns the user to the page they were on! here's my implementation:
and there we go!
the code that gets the file name from the full url isn't bug proof. for example if query strings are involved with un-escaped '/' in them, it will fail.
However there are many scripts out there to get the filename from url!
Happy Coding!
Alex
Yeah, you'd need to have a javascript function triggered by an onclick that does an AJAX load of a page and then returns false, that way they won't be redirected in the browser. You could use the following in jQuery, if that's acceptable for your project:
You could also do a post-back if you need to use form values (use the $.post() method).
There is the only better way is AJAX as everyone is suggest in their posts. The alternative is using IFrames like below:
Now you will get the output in IFrame (you can place IFrame wherever you need in the page or event hide it and the result from the script).
Hope for non Ajax solution this is better.