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