472,145 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,145 software developers and data experts.

use a javascript variable in php?

Hi at all!

I want to use a javascript variable in php. The reason is that i want
to know the client's screen resolution.
Here is the code i'm using:

<head>
<script language='JavaScript'>
<!--
var a=(screen.width-750)/2;

function getvariable(val) {
var dummy = eval(val);
document.write(dummy);
}
// -->
</script>
<?php
function get_JS_var($js_var_name) {
$x = "<script> getvariable('" . $js_var_name . "'); </script>";
return $x;
}
?>
</head>
<body>
<?php
$screen_pos = get_JS_var("a");
$b="left:".$screen_pos."";
?>
<div style="position:absolute; <?print $b;?>; top:0; z-index:4">
etc....
</body>

The problem is that if i output the $b outside the div element, it
displays to the browser the right result. But inside the div element
it outputs the
left:<script> getvariable('a'); </script> and so the layer apears in
wrong place.

Any ideas about this or other way to manage to output the div content
in a place according to the client's screen resolution?

Thanks in advance.
Jul 17 '05 #1
12 25049
On 28 Jun 2005 15:47:34 -0700, mb*****@gmail.com (mbasil7) wrote:
I want to use a javascript variable in php.
This is more complicated than it may appear.
The reason is that i want
to know the client's screen resolution.
Here is the code i'm using:

<head>
<script language='JavaScript'>
<!--
var a=(screen.width-750)/2;

function getvariable(val) {
var dummy = eval(val);
document.write(dummy);
}
// -->
</script>
<?php
function get_JS_var($js_var_name) {
$x = "<script> getvariable('" . $js_var_name . "'); </script>";
return $x;
What are you expecting this to do?
}
?>
</head>
<body>
<?php
$screen_pos = get_JS_var("a");
$b="left:".$screen_pos."";
?>
<div style="position:absolute; <?print $b;?>; top:0; z-index:4">


This isn't using a Javascript variable in PHP, this is outputting invalid CSS
within HTML (trying to put a <script> tag in a style).

You seem to be badly confused as to the execution order and execution location
of PHP versus Javascript.

In this case it appears you're better off leaving PHP out of the picture and
just doing this all in client-side Javascript?

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #2
mbasil7 wrote:
Hi at all!

I want to use a javascript variable in php. The reason is that i want
to know the client's screen resolution.


How about this:

--- Code ---
<?php
if (!isset($width) || !isset($height))
echo "<script type='text/javascript'>
document.location='index.php?width='+screen.width+ '&height='+screen.height;
</script>";

echo "$width X $height";

?>
--- End Code ---

I don't have the ability to test it right now, so don't bash me too much
if it doesn't work. However the idea should be pretty sound.
Jul 17 '05 #3
Client-side Javascript is interpreted in the browser after the server
has processed the PHP, so you can't process Javascript in this way
inside the PHP. There are workarounds, however...

On your login page of your web app, you could capture the onSubmit
event in the FORM html tag, then have a function update some hidden
input vars and then do a return true. That posts this to the next page
and you can read, with PHP, the values from the hidden input vars. Just
a couple days ago I wrote an article in comp.lang.php on a timezone
problem and how I resolved it, and I used this technique with
Javascript.

Or, you could have your index.php have some javascript that detects it
is the first time the user has come to your web page because they don't
have a persistent cookie that says, for instance, "screensize". You
could detect this with Javascript and then write this cookie with
Javascript. When the following PHP pages in your app run, they can read
this cookie and process it accordingly. Or, the other option is that
once this cookie is set, you can tell the browser to refresh back to
itself again so that the index.php page can immediately pick up the
cookie and process it again.

Or, you could use Javascript to tell the page to redirect back to
itself but pass some query params for screen width and height. You
could then parse the query params in PHP.

Jul 17 '05 #4
Didn't the server return browser information in the superglobal
$_SERVER array?

Jul 17 '05 #5
I have knowledge of them that you wrote. Simply i wonder if there is a
way to do this without refresh the page or using a cookie.
Probably, the problem will solve if i used javascript only for my div
element.

Thanks all for your responses.
Jul 17 '05 #6
cyberhorse wrote:
Didn't the server return browser information in the superglobal
$_SERVER array?


Yes there is some browser info, but as far as I know, the screen size is
not included.
Jul 17 '05 #7
mbasil7 wrote:
I have knowledge of them that you wrote. Simply i wonder if there is a
way to do this without refresh the page or using a cookie.
Probably, the problem will solve if i used javascript only for my div
element.


You cannot pass javascript variables to php for the reasons mike stated,
but if you just want to display the screen size, or resize your div
element according to the screen size, that is totally possible. To pass
variables to CSS, you don't need to pass them to php first.

I just re-read your fisrt post, and probably figured out what you want
to do. Let's see if we can find a fix for it.

--- Code ---

<head>
<script type='text/javascript' language='JavaScript'>
<!--
function moveDiv(id) {
d = document.getElementById(id);
a = (screen.width-750)/2;
d.style.left = a+"px";
}
// -->
</script>
</head>
<body onload="moveDiv('divthingy');">
<div id="divthingy" style="position:absolute; left:0; top:0; z-index:4">
etc....
</body>

--- End Code ---

That should work, again untested. As you see there is no php needed at all.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFCwoEoZeVxmdI1of8RAlNLAJ4x0FFs9dKtw5Iq7nz6HN wOYihFJgCgr73Q
N+iPa5mE1bfwb32va1BvfEE=
=yJp1
-----END PGP SIGNATURE-----

Jul 17 '05 #8
mb*****@gmail.com (mbasil7) wrote in news:fdf7cf3e.0506281447.6ff0f5a6
@posting.google.com:
Hi at all!

I want to use a javascript variable in php. The reason is that i want
to know the client's screen resolution.


hold up the PHP and go back to HTML/Web design basics.

You should NEVER design a site based upon screen resolution. EVER. Learn
how to write flexible HTML/XHTML/CSS before embarking on PHP.
Jul 17 '05 #9
PHP is a server-side scripting language. This means that it is executed on
the web server, and the result of this execution is the HTML that is sent to
the client (web browser).

Javascript is run on the client machine by the web browser (Firefox, IE,
etc).

So - you can access PHP variables in your Javascript - or more correctly,
you can use PHP to set Javascript variables, but you cannot access a
Javascript variable directly in PHP, because the Javascript is just
meaningless text until it reaches the web browser, at which point the PHP is
all done executing and is no longer in the picture.

This said, there IS at least one way for you to access to your Javascript
variables (indirectly) from your PHP scripts. It involves the following
steps:

1. Start a session in your PHP script. Check if the $_SESSION["scriptvars"]
array or $_POST array is set, and if it is, that your desired variables are
contained within either. If they are in your session, continue with your
regular PHP script. If they are in your $_POST array, set your session array
with them and continue with your regular PHP script. This will make more
sense in a minute.
2. If they are not set, then you will need to "fetch" them. This is done by
sending a blank html document with a hidden form and an onload=getvars() in
the <body> tag.
3. Write your JavaScript getvars() function to set the values of all the
(hidden) fields in your form - such as screen size, window size, etc. When
this page loads in the function will be executed. The last line in the
function should be document.formname.submit(); which will submit the form.
Use method=POST and target=yourphpfilename.php.

Note that this approach can have an undesirable effect on search engines as
they will likely only see the blank page used to fetch the variables
available to Javascript.

ECRIA
http://www.ecria.com
Jul 17 '05 #10
Thans a lot all of you for your responses. Finally i found the
solution for my problem. Simply my first thought was to use a
javascript variable in php. But in that way i must refresh the page at
least one time.
The solution is to use javascript to "echo" the div element only. In
this way i can access the javascript variable and output the layer in
the right place in the browser window. Here is the code i finally use:
<head>
..
..
..
<script language="JavaScript" type="text/JavaScript">
<!--
var pos;

if (window.innerWidth)
{
pos =window.innerWidth;
}
else if (document.documentElement &&
document.documentElement.clientWidth)
{
pos= document.documentElement.clientWidth;
}
else if (document.body)
{
pos= document.body.clientWidth;
}
//-->
</script>
..
..
</head>

<body>
..
..
..
<script language="JavaScript" type="text/JavaScript">
<!--
if (pos>750) {
document.write ('<div id="html" style="position:absolute;
left:'+(((pos-750))/2+293)+'; top:19; z-index:4;
visibility:hidden;">');
} else {
document.write ('<div id="html" style="position:absolute; left:293;
top:19; z-index:4; visibility:hidden;">');
}
//-->
</script>
..
..
..
</body>
</html>
I specify that the javascript code in the head section gives to the
pos the size of the browser window.
Jul 17 '05 #11
AMEN!
-J
Good Man wrote:
mb*****@gmail.com (mbasil7) wrote in news:fdf7cf3e.0506281447.6ff0f5a6
@posting.google.com:

Hi at all!

I want to use a javascript variable in php. The reason is that i want
to know the client's screen resolution.

hold up the PHP and go back to HTML/Web design basics.

You should NEVER design a site based upon screen resolution. EVER. Learn
how to write flexible HTML/XHTML/CSS before embarking on PHP.

--
___________________
Jay
Oct 19 '05 #12
But if you wanted to import a javascript variable, you can put it in a
form as a hidden field and submit it to the process php page and then
access it by $_POST[].

There may be an easier way, but that is how I have done in in the past.
I have yet to find a need for it in any of my current projects, so this
method may be obsolete.

But like Jason said, nothing screams 'amateur' then a site that says
"Best viewed in...."

Jason Hodges wrote:
AMEN!
-J
Good Man wrote:
mb*****@gmail.com (mbasil7) wrote in news:fdf7cf3e.0506281447.6ff0f5a6
@posting.google.com:

Hi at all!

I want to use a javascript variable in php. The reason is that i want
to know the client's screen resolution.


hold up the PHP and go back to HTML/Web design basics.
You should NEVER design a site based upon screen resolution. EVER.
Learn how to write flexible HTML/XHTML/CSS before embarking on PHP.



--
Scott Johnson
http://www.seaforthsailingclub.com
Oct 19 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by ms_chika | last post: by
3 posts views Thread by mbasil7 | last post: by
2 posts views Thread by pleaseexplaintome | last post: by
reply views Thread by Saiars | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.