Hi, I am currently trying to plan the Explorer layout for adding some GIF images. I am able to query the height and width of the explorer using JS, below commands:

HEIGHT:

$height = '<script type="text/javascript">
if (window.innerHeight) //if browser supports window.innerWidth
document.write(window.innerHeight)
else if (document.all) //else if browser supports document.all (IE 4+)
	document.write(document.body.clientHeight)
</script>';

WIDHT:

$width = '<script type="text/javascript">
if (window.innerWidth) //if browser supports window.innerWidth
document.write(window.innerWidth)
else if (document.all) //else if browser supports document.all (IE 4+)
	document.write(document.body.clientWidth)
 </script>';

Now if I run echo "width<br>"; <-- It returns the correct width of the explorer.

But if I do an edit to this value in the PHP code, for example: $width = $width - 200; echo "$width<br>"; <-- This returns - 200

Can any give some insight why I am unable to make the subtraction correctly here?

Recommended Answers

All 3 Replies

because in your first statement, the php and javascript is being evaluated when the page is executed. The php $width variable does not contain the 'value' you think is there. It contains the javascript statement that is being evaluated when you load your page. Look at your source code to see this. $width in your code above is a string of text that happens to evaluate to a number once the javascript contained within is actually written to your page.

if you need to pass variables from javascript to php you must do a page reload or refresh and use $_GET or $_POST parameters.
my page was named test5.php to get the values into your php you 'could' do something like this...

<script type="text/javascript">
function set_stuff() {
	if (window.innerWidth) //if browser supports window.innerWidth
	  var w = window.innerWidth;
	else if (document.all) //else if browser supports document.all (IE 4+)
	 var w = document.body.clientWidth;
	if (window.innerHeight) //if browser supports window.innerWidth
	var h = window.innerHeight;
	else if (document.all) //else if browser supports document.all (IE 4+)
	var h = document.body.clientHeight;
	// this will redirect you to 'test5.php' with width and height values set.  in turn causing if statement to be true.
	location.href="test5.php?width="+ w +"&height="+h;
}
</script>
<?php
// first time through does not evaluate, you will hit the else statement and fire the above js set_stuff code.
$w = '';
$w = $_GET['width'];
$lw = $w - 200;
$h = '';
$h = $_GET['height'];
$lh = $h - 200;
if ($w != '') {
	echo "<br> width  = " . $w . " <br />";
	echo "<br>smaller width  = " . $lw . " <br />";
	echo "<br> height  = " . $h . " <br />";		
	echo "<br>smaller height  = " . $lh . " <br />";		
} else {
?>
<script type="text/javascript">
// this will call your set_stuff js function if the variables are not set.
set_stuff();
</script>
<?php
}
?>

Thank you, this was really useful to understand and also to achieve what I was looking for.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.