I need help regarding PHP and Javascript!!!?

ikki-kun

New member
hey guys!

how can I get the value of a certain variable in a function in a javascript?

I want the value to appear using the 'echo' syntax in PHP...

ex.
<script>
function sampleFunc()
{
num++;
}
</script>

how can i get that 'num' variable and display it using php!?
anyone who could help???!

Thanks!
 
You can't do that, you can only do it the other way around.

You can't display a JS variable with PHP simply because PHP only runs on the server side. By the time the HTML and JS page is created, PHP has finished running and the page was sent to the client.

JS has no interaction whatsoever with PHP and it doesn't need to.
THe only way you can display JS variables in a HTML page is by using the innerHTML property of a div or by setting the value on a input tag.

THe only way to send js variables on to the server so PHP can use them is through AJAX (attaching the variable value to a HTTP request in the background - but in this case displaying it with echo makes no sense since the user can't see it).

You can, however, pass a variable from PHP to JS like this:

var myVar = "<?=$myPHPVar?>";

When the page has been passed through the PHP interpreter on the server, the resulting HTML and JS is sent to the client to run. At that time, myVar will get the value that results after running the PHP code.
 
Back
Top