Forum Moderators: open
<script language="javascript"><!--
for(i=0; i<=1; i++){
var message="<?php echo $t[i];?>"; //quotes outside php
document.write(message);
}
//-->
</script>
But, one key reason for using this construct is to use a javascript variable in the php request--and this doesn't work for me. For example in the script:
<script language="JavaScript">
<!--
var loginName="myfile.txt";
var command = "<?php $fh =" + loginName + "; $infile=file($fh); for($i = 0;$i<count($infile);$i++){ $test = $test . $infile[$i];}?>";
document.write (command);
var buddylist='<?=$test?>';
<script>
...PHP never seems to get the value of "loginName". Instead it says that it can't find the file called "loginName". This holds true whether I use single or double quotes.
How do you get the value of a javascript variable into the PHP line? (by the way, for this application, I can't do it all on the server with PHP)
That said, server side scripting is processed before the page is rendered. By the time your j/s runs on the client, it's too late to set a PHP command. PHP needs to have the value before the page starts writing.
I hope that makes sense. Someone else will probably explain this far better.
Generally, you will pass Javascript variable back to PHP on the server by sending it as part of a request.
For example:
var giveToPHP="forPHP.php?loginName="+loginName;
buddyFrame=document.getElementById("buddyFrame");
buddyFrame.src=giveToPHP;
//might be buddyFrame.location.href ... sorry I'm not certain
Javascript can pass variables to PHP by sending a request, and PHP can pass variables to Javascript by compiling info before delivering a request.