Click to See Complete Forum and Search --> : Include PHP Variables in JS


Warli
06-10-2003, 03:03 PM
Hi,

i want a tool to find out the screen.width of your Browser and give up an "php variable" after the right resolution

Example:

<SCRIPT language="JavaScript">

if (screen.width >= "1001") $screen=1;
if (screen.width <= "1000") $screen=0;

</script>

"screen" = the PHP Variable


---> PHP Script:
<? PHP
echo $screen;
?>


.. if the resolution over 1000, screen must be 1 and otherwise screen must be 0.

The problem: the Javascript dosent work. And the variable "screen" in the JS script dosent convert to an php variable.

THX a lot
warli

pyro
06-10-2003, 03:55 PM
You have two ways of getting a javascript variable into a PHP page. One is submitting a form, and the other is loading a page with a query string. For this, I would recommend the second. Please look at the following code:

<script type="text/javascript">
if (screen.width > "1000") {
top.location.href= "yourpage.php?screen=1";
}
else {
top.location.href= "yourpage.php?screen=0"
}
</script>

And then use this in your PHP:

$screen = $_GET["screen"];

SearedIce
06-10-2003, 05:32 PM
wow thats interesting...

would it work with asp also?

~John

pyro
06-10-2003, 05:39 PM
Yes, the concepts remain the same, though the ASP sytax would be different. The javascript syntax would remain the same.

Warli
06-11-2003, 10:56 AM
THX the code is fine but the Problem are:

The JS Script and the php Code must be on the same site.

If the JS and the code on the same site, an endless loop starts, because the page and the JS will ever load again and again.

so is there an another way?

please help
thx
warli

pyro
06-11-2003, 11:00 AM
Yep, in your PHP, do this:

<?PHP
if (!isset($_GET["screen"])) {
echo "<script type=\"text/javascript\">
if (screen.width > 1000) {
top.location.href= \"yourpage.php?screen=1\";
}
else {
top.location.href= \"yourpage.php?screen=0\";
}
</script>
}";
?>That will only run if the variable isn't already set.