Forum Moderators: open

Message Too Old, No Replies

reading php in javascript

conditional variable assignment

         

sssweb

5:08 pm on Mar 8, 2006 (gmt 0)

10+ Year Member



I have some javascript code I use to determine the user's screen resolution, and I want to assign a PHP variable on the basis of that.

<script language="JavaScript1.2" type="text/javascript">
<!--
if (screen.width<1024) { document.write("<?php $var = 1;?>");
}
else { document.write("<?php $var = 2;?>");
}
// -->
</script>

What I've found though, is that PHP reads both statements regardless of screen size, and sets $var equal to the last value it finds (in this case, '2').

Is there a way to make this work conditionally?

DrDoc

5:12 pm on Mar 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The PHP code is executed prior to the JavaScript code. There is no way of using JavaScript to set a PHP variable (although I wish there was).

What you can do, however, is use JavaScript to set a cookie with the information. Then, on subsequent page views you can let PHP read the cookie to retrieve the variable. This cookie will not be available until the second page view.

sssweb

5:31 pm on Mar 8, 2006 (gmt 0)

10+ Year Member



Thanks, I'll try to work around it.