Calling Javascript Within Php Block And Vice-versa
Solution 1:
This assumes that you are using jQuery...
<ahref='javascript:delete();'>Delete</a><scripttype="text/javascript">functiondelete()
{
$.post("/your_script.php", {}, function(result) {
});
}
</script>
Solution 2:
JavaScript functions execute on the client (in the browser) and PHP executes on a server. So, the JavaScript must send a message - via HTTP - to the server to be handled by PHP. The PHP would perform the delete. Make sense?
The message sent to the server might be sent via AJAX.
Solution 3:
Maybe you should use Ajax: http://en.wikipedia.org/wiki/Ajax_%28programming%29
Solution 4:
PHP is a server-side technology, while JS is a client-side. They cannot interact with each other - in other words: they're completely independent.
PHP can only output code that is a JS code:
echo'document.getElementById("test").appendChild(document.createTextNode("' . $myVar . '");';
It's all PHP can do. JavaScript cannot direct interact with PHP as well. You'll have to use AJAX to send a new HTTP request and process returned data.
Solution 5:
PHP is a server-side language, thus you can not output PHP script to the browser and expect that it will parse it with the PHP engine.
What you're looking for is probably AJAX, or simply redirecting the user to another page (with different URL parameters) or submitting a form. AJAX doesn't require from the browser to reload the page, while the two other methods does.
Anyway, you can execute a JS script with the "onclick" method, that's executed when the user clicks on the element: <a href="#" onclick="myFunc();">Delete</a>
But the following approach looks better and considered as an ideal one:
<ahref="#"id="myId">Delete</a><scripttype="text/javascript">document.getElementById("myId").onclick = myFunc;
</script>
Post a Comment for "Calling Javascript Within Php Block And Vice-versa"