Page 1 of 1

Javascript and PHP variable passing

Posted: Thu Feb 13, 2003 10:20 pm
by lvzrdoz
:?: I'm attempting to change variable contents based on screen width. I use Javascript screen.width to assign value to $REZ. This works. However, I don't know why I can't seem to compare values correctly using $REZ.

Here are the results I got with an 800x600 screen:

REZ=800! and variable is UNCHANGED!

Suggestions?

----------------------------- code ------------------------
<HTML><HEAD>
<?php $REZ = "<script language=javascript>
document.write(screen.width); </script>" ; ?>
</HEAD><BODY>
<?php
$wyde = "UNCHANGED!" ;

if( $REZ == 800 ) $wyde = "changed to Numeric 800";
if( $REZ == "800" ) $wyde = "changed to Alpha 800";

echo "REZ=" . $REZ . "! and variable is " . $wyde;
?>
</BODY></HTML>

Posted: Fri Feb 14, 2003 1:14 am
by volka
php is running server-side only producing a stream of data. How this is interpreted is up to the requesting client.
If you have something like

Code: Select all

<?php echo '<html><body>test</body></html>'; ?>
you hope, the client is a browser that interprets it as html. But it makes to difference to php if you have something like

Code: Select all

<?php echo '123456'; ?>
Anyway it is processed server-side and only the output of that script is transfered to the client, which is (almost) not aware that there was php involded to create the document.
You codeblock
<?php $REZ = "<script language=javascript>
document.write(screen.width); </script>" ; ?>
(if working) would suggest two things. 1) php is running at the client-computer without beeing installed. 2) it has access to the document object model (any method) of the client-program. Both I would find disturbing ;)
This script

Code: Select all

<html><body>
   <script language="text/javascript">
<?php
$a = 1+1;
echo 'javascript:alert("', $a, '");';
?>
   </script>
</body></html>
is undistingishable from the simple document

Code: Select all

&lt;html&gt;&lt;body&gt;
   &lt;script language="text/javascript"&gt;
       'javascript:alert("2");
   &lt;/script&gt;
&lt;/body&gt;&lt;/html&gt;
since it produces the same output (server-side) or input (client-side).
There is no connection between the server-side script and the client-side engine between each request. In fact the php-script ends as soon as the document is delivered and the internet connection may have been shutdown without any difference.
If you want to pass parameters back to php you have to invoke a new document request. Your script will start again producing a new document that is transfered back (mostly everytime) replacing the current client-document.
You can use javascript to create a new request, e.g.

Code: Select all

&lt;html&gt;
	&lt;head&gt;&lt;title&gt;changing resolution&lt;/title&gt;
		&lt;script type="text/javascript"&gt;
			function ReloadRes(paramRes)
			&#123;
				document.location = "page.php?res="+paramRes;
			&#125;
		&lt;/script&gt;
	&lt;/head&gt;
	&lt;body&gt;
		loding page for 
		&lt;script type="text/javascript"&gt;
			var sRes = screen.width + "x" + screen.height;
			document.write(sRes);
		&lt;/script&gt;	
		. Please wait.
		&lt;script type="text/javascript"&gt;
			ReloadRes(sRes);
		&lt;/script&gt;
	&lt;/body&gt;
&lt;/html&gt;
and the server-side script page.php would receive the parameter res via get, for newer version of php (see also: http://forums.devnetwork.net/viewtopic.php?t=511) this would be stored in $_GET['res']