473,883 Members | 1,912 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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='JavaS cript'>
<!--
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:".$scr een_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 25163
On 28 Jun 2005 15:47:34 -0700, mb*****@gmail.c om (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='JavaS cript'>
<!--
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:".$sc reen_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.andyhsoftwa re.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.locati on='index.php?w idth='+screen.w idth+'&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='JavaS cript'>
<!--
function moveDiv(id) {
d = document.getEle mentById(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

iD8DBQFCwoEoZeV xmdI1of8RAlNLAJ 4x0FFs9dKtw5Iq7 nz6HNwOYihFJgCg r73Q
N+iPa5mE1bfwb32 va1BvfEE=
=yJp1
-----END PGP SIGNATURE-----

Jul 17 '05 #8
mb*****@gmail.c om (mbasil7) wrote in news:fdf7cf3e.0 506281447.6ff0f 5a6
@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.formna me.submit(); which will submit the form.
Use method=POST and target=yourphpf ilename.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
26054
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
12620
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. *** Sent via Developersdex http://www.developersdex.com ***
4
12335
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
5732
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 the code i'm using: <head> <script language='JavaScript'>
3
6799
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? For example, it seems that the following should work, but it doesn't appear to. Is this valid code? CGI code --- get_string.pl ------ print "Content-type: text/plain\n\n";
6
2365
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, which has a blank page with a script block containing the variable I need access to. However, when trying to access the variable, I get access is denied/permission is denied error, due to the site being on another domain (my page is actually...
2
4891
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 #!/ois/usr/bin/perl -w use strict; use CGI qw(:standard); my $cgi=new CGI; my $flg=0;
3
2409
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 to a perl variable, how it is possible?(Without using Ajax). Urgent friends
7
3234
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. Onmouseover javascript read area names and send it to javascript generated page. On generated page, area name (from first page) need to be query variable (depending of them show data from database). If i put string instead of variable,everything works fine,...
1
1726
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> this is possible. But i want <% String Str =// javascript variable value ;
0
11128
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10734
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10838
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10408
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7964
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7119
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5794
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5988
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3230
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.