my PHP :
<?php
function myFunc($echo) {
//this js will print display the value of "myvar"
$myvar = "<script type='text/javascript'>
var myvar= true;
document.write(myvar);
</script>";
//$myvar = "aDuck";
if ($echo) {
echo $myvar;
}
}
function foo($echo) {
if ($echo) {
//this echoes "true", which is the value of js "myvar"
echo "true";
}
}
if (myFunc(true) == foo(true)) {
echo "<br> WOW! it works!";
}
?>
I want PHP to test the value of a javascript variable in the same file without refreshing the page. I know there are ways to pass js vars to php through multiple files (ajax, json...) but I was curious and wanted to know if this could possibly work. It does, but I am not satisfied because PHP actually executes the functions.
Here's the output :
truetrue
WOW! It works!
The actual condition that is tested seems to be "this function echoes true
".
So now for the questions :
- How do I prevent this from echoing
true
? - Is there a way to make PHP test
myFunc
andfoo
without
actually executing those functions ? - As the javascript is loaded with the page, is there a way for the
user to modify
myvar
before it is read by the PHP script ?