473,399 Members | 3,603 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 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 25124
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Matt | last post by:
If I assign Java variable a to javascript variable x, it is fine. <% int a = 10; %> var x = <%= a %>; alert(x); But if I do the other way around, then it has 500 error. any ideas??
5
by: ms_chika | last post by:
Hi to all, I have this problem in xsl wherein i want to access a variable in javascript and use it my xsl. How would i access or use a javscript variable in my xsl file? Please help. ...
4
by: tnhoe | last post by:
Hi, if I have both javascript and aspscript, how to get/retrieve the variable value in javascript into asp variable ? regards hoe
3
by: mbasil7 | last post by:
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. Keep in mind that i am not a javascript programmer but php. Here is...
3
by: stahl.karl | last post by:
I have a CGI/Perl program that returns a string output. Is it possible to get this into a Javascript variable, where the name of the variable is defined in the Javascript and not in the Perl code?...
6
by: leppert | last post by:
Hello, I am trying to access a javascript variable on another site. The site requires the user to be logged in, so what I am doing is submitting a form and redirecting the output to an IFRAME,...
2
by: pleaseexplaintome | last post by:
Hi I have the following perl/cgi script snippet. The goal of this script is to pass a javascript variable to perl where it can be re-used later. Any help is appreciated, Thanks ...
3
by: sasimca007 | last post by:
Hello friends, IN modperl we write perl with html,javascript and etc. when we are writing perl code in the middle of javascript and if we want to assign a javascript variable...
7
by: Arsale | last post by:
I am trying to pass a javascript variable to php on javascript generated page, but on the new page I got only "Array". Where did I go wrong? On first page, i've got image map with areas....
1
by: shinu bhaskar | last post by:
how to assign a JavaScript variable to a jsp variable in a single page Ex : <script type="text/javascript"> var MM=Calculate_Form('<%=DATE_INSTALL%>','<%=TILLDATE%>'); alert(MM); </script>...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.