/    Sign up×
Community /Pin to ProfileBookmark

Include PHP Variables in JS

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

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@pyroJun 10.2003 — 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:

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


And then use this in your PHP:

[code=php]$screen = $_GET["screen"];[/code]
Copy linkTweet thisAlerts:
@SearedIceJun 10.2003 — wow thats interesting...

would it work with asp also?

~John
Copy linkTweet thisAlerts:
@pyroJun 10.2003 — Yes, the concepts remain the same, though the ASP sytax would be different. The javascript syntax would remain the same.
Copy linkTweet thisAlerts:
@WarliauthorJun 11.2003 — 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
Copy linkTweet thisAlerts:
@pyroJun 11.2003 — Yep, in your PHP, do this:

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

Success!

Help @Warli spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.3,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...