I would like to call a php function in javascript. I have one solution, which actually works, but not the way i would like.
code:
function test(){
document.getElementById("php_code").innerHTML="<?php
print 'something';
?>";
}
...
<a href="#" onclick="test(); return false;"> test </a>
It works when i click on the link, but i can't call the funciton in other way, just by clicking on the link. So what i would like, to call the function simply by test()
learn AJAX (Asynchronous Javascript And Xml), as javascript is executed AFTER the PHP. With AJAX you can without refreshing the page send a request to the server, and the server returns some string then, which you could use on your page. (By sending some parameters with the request you can execute the test() function (if you convert it to PHP) with a simple if-statement.)
PHP is executed on serverside (it is responsible for rendering the page for the browser), meanwhile Javascript will be executed clientside (after th page has been delivered to the browser).
So the only way, you could pass values between them, is the one of your solution. Calling functions between them is not possible.
What you are trying to do is not possible.
PHP is server-side code and Javascript is client-side. The only method of executing a PHP script from javascript call is via AJAX. There is a very simple tutorial to get you up and running with AJAX on w3schools.