views:

1419

answers:

7

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?

+2  A: 

If I understand correctly, you will need AJAX.

http://www.w3schools.com/Ajax/Default.Asp

+1  A: 

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.

Chacha102
"You cant run PHP when a user clicks on a link unless you use AJAX."Eh?
Al
Exactly what I said. Unless you request a new page, you can't run PHP.
Chacha102
PHP is only ran when pages are requested.
Chacha102
Yes, but you can request a PHP page without AJAX. AJAX just lets you request it without leaving the page you're on. Your wording makes it sound like AJAX is required for *all* uses of PHP.
ceejayoz
Ok, I fixed the wording. I thought it was a given that he was trying to not leave the page, but I guess some people don't feel like thinking.
Chacha102
+5  A: 

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).

Parrots
hah beat me by 2 seconds, so u get upvote!
Roy Rico
you should prob also return false from the function, so the HREF isn't followed.
Roy Rico
+2  A: 

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
}

http://docs.jquery.com/Ajax/jQuery.ajax

Roy Rico
No reason to downvote the guy just because he didn't use jQuery. Someone should probably edit the post to fix the link though.
Justin Poliey
Thanks justin, fixed the link. I just added the link to jquery, but not the actual code just in case it wasn't an option. Not everyone gets to use Jquery, no matter how much they want to (stupid legal contracts!)
Roy Rico
You should at least add the non-jquery AJAX code example within your function, as it is you're just documenting the onclick functionality of javascript, which he already knew about :P
Parrots
A: 

"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?

Al
"Why are they clicking a link if it doesn't take them anywhere?" Uh... you're on a site that lets you upvote, downvote, add comments, etc. all in exactly that manner. Haven't you noticed?
ceejayoz
A: 

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>
Tom
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.

el