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?
If I understand correctly, you will need AJAX.
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.
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:
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
function doSomething() {
$.get("somepage.php");
return false;
}
</script>
<a href="#" onclick="doSomething();">Click Me!</a>
You could also do a post-back if you need to use form values (use the $.post() method).
as others have suggested, use javascript to make an ajax call.
<a href="#" onclick="myJsFunction()">whatever</a>
<script>
function myJsFunction() {
// use ajax to make a call to your PHP script
// for more examples, using Jquery. see the link below
return false; // this is so the browser doesn't follow the link
}
"I want to have a page run some PHP code when a user clicks on a link"
Link to the PHP script
"without redirecting them"
Why are they clicking a link if it doesn't take them anywhere?
either send the user to another page which does it
<a href="exec.php">Execute PHP</a>
or do it with ajax
<script type="text/javascript">
// <![CDATA[
document.getElementById('link').onclick = function() {
// call script via ajax...
return false;
}
// ]]>
</script>
...
<a href="#" id="link">Execute PHP</a>
in answer to your question, AI, i have a website for my photos that allows the user to click on a link which runs a php script to return all the thumbnails in the proper folder (each folder is a different collection of photos) and place them on the webpage. i was hoping to merely replace the set of thumbnails, though, without creating a new web page for each collection, which would make it easier to build on the page as i add additional new collections-- i wouldn't have to create new websites; i could just add new links.
it sounds like AJAX is the answer. from your link above:
AJAX is the art of trading data with a web server, and changing parts of a web page, without reloading the whole page.
thanks, guys.