call php function from image onclick?

Go To StackoverFlow.com

0

i am a beginner in php scripting... can I call php function from img tag by using onclick like this?

<img src="3.png" onclick="myfunction();" />

I tried this:

<a href="vote.php"><img src="3.png" /></a>

but it is not working. I do not want to go the the vote.php page. Any ideas ?

2012-04-05 22:17
by user1200640
No. Look into AJAX - Shadow Wizard 2012-04-05 22:19
possible duplicate of how to call a php function on button click and (even better) Javascript and PHP functions ... please use the search before you ask a new question - Felix Kling 2012-04-05 22:23


2

I think you need to go back to some basic concepts there. PHP is a server side language, it's interpreted on the the server and rendered as html on the client side.
Javascript on the other hand is interpreted by the browser and is what you'd use in this case. onclick is a js event.

2012-04-05 22:20
by elclanrs
s/revise/revie - Kristian 2012-04-05 22:21


1

Sortof. Your onclick event should have an ajax call inside of that which loads a php file. Then whatever would have been printed / echo'd by the php file will be in the ajax response.

function myFunction() {
   $.ajax({
      url: 'vote.php',
      data: {
         vote: 1,
         id: 3
      },
      success: function( response ) {
         //do whatever with the response
      }
}

If you use jquery (or mootools) for the ajax call, it will be simpler than a regular javascript ajax request. jquery is used above.

Documentation: http://api.jquery.com/jQuery.ajax/

2012-04-05 22:21
by Kristian


0

Another option that doesn't require any Javascript is to add an iframe and send the vote request there

<a href="vote.php" target="voteframe"><img src="3.png" /></a>

<iframe id="voteframe" style="/*display:none*/" />
2012-04-05 22:27
by j13r
Oh man, this brings back memories. Personally, I'd recommend an AJAX call with full fallback (which all AJAX calls should have) as this method will give you the option of notifying the user properly, without hacking around an iframe. But, this solution does work and I have to say I've used it in the past (early 2000's) - 0b10011 2012-04-05 22:34
Ads