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 ?

1

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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/

Comments

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*/" />

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.