Forum Moderators: coopster

Message Too Old, No Replies

How to do correct Javascript with PHP?

         

toplisek

7:06 pm on Jan 30, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I have code (Javascript) if PHP is not detected.

1. How to do correct validated code, specially in echo?
2. Is possible to store into variable prepared for PHP MYSQL when I detect Javascript way my resolution?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
</head>

<body>
<script type="text/javascript">
location.replace('http://www.mydomain.com/index.php?w=' + screen.width + '&h=' + screen.height );
</script>

<?PHP
$_SESSION['width'] = $_GET['w']; // store session data
$_SESSION['height'] = $_GET['h']; // store session data

if ($_SESSION['width']!=null){
echo ' function ShowScreenResolution(){
var w = screen.width;
var h = screen.height;
alert( "Your screen resolution is " + w + 'x' + h)}';
}
?>
</body>

</html>

rocknbil

9:08 pm on Jan 30, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is this what you're asking? It should work . . .

<?php
$_SESSION['width'] = $_GET['w']; // store session data
$_SESSION['height'] = $_GET['h']; // store session data

if ($_SESSION['width']!=null){
echo '
<script type="text/javascript">
function ShowScreenResolution(){
var w = screen.width;
var h = screen.height;
alert( "Your screen resolution is " + w + 'x' + h);
}
</script>
';
}
?>

Note I moved the closing '; *after* the function end and just added the script tags. One has to wonder, though, why you don't just get the viewport dim's on "this page". You're using JS to access them, so you might as well grab them here instead of grabbing them then redirecting. (?)

toplisek

9:36 am on Jan 31, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Your code shows an error:
expecting; or , in line 25....alert( "Your screen resolution is " + w + 'x' + h);

rocknbil

4:40 pm on Jan 31, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, escape the single quotes then.
\'x\'