473,387 Members | 1,540 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,387 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.
Keep in mind that i am not a javascript programmer but php.

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 23 '05 #1
3 5689
mbasil7 wrote:
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'>
<!--
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.


The only way you will be able to use a Javascript-generated variable in
PHP is by sending another request to the server. By the time the
javascript executes, php has already done it's job. To do this, you may
want to do something like:

<html>
<head>
<script type="text/javascript">
<!--
getJsValue = function (){
var a=(screen.width-750)/2;
var url='<?php echo $_SERVER['PHP_SELF'] ?>?a='+a;
document.location.href=url;
}
-->
</script>
</head>
<body <?php if(!isset($_GET['a'])) echo 'onload="getJsValue(); return
true;"'; ?>>
<?php
if(isset($_GET['a'])){
echo 'Javascript value: ',$_GET['a'];
}else{
echo 'refreshing';
}
?>
</body>
</html>

HTH

--
Justin Koivisto, ZCE - ju****@koivi.com
http://koivi.com
Jul 23 '05 #2
Justin Koivisto <ju****@koivi.com> wrote in message news:<Kt***************@news7.onvoy.net>...
mbasil7 wrote:
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'>
<!--
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.


The only way you will be able to use a Javascript-generated variable in
PHP is by sending another request to the server. By the time the
javascript executes, php has already done it's job. To do this, you may
want to do something like:

<html>
<head>
<script type="text/javascript">
<!--
getJsValue = function (){
var a=(screen.width-750)/2;
var url='<?php echo $_SERVER['PHP_SELF'] ?>?a='+a;
document.location.href=url;
}
-->
</script>
</head>
<body <?php if(!isset($_GET['a'])) echo 'onload="getJsValue(); return
true;"'; ?>>
<?php
if(isset($_GET['a'])){
echo 'Javascript value: ',$_GET['a'];
}else{
echo 'refreshing';
}
?>
</body>
</html>

HTH


Justin thanks a lot for your answer.

Finally i find a solution for my problem. It was very easy and i
didn't needed to use any Javascript-generated variable in PHP.

I use the
document.write ('<div id="html" style="position:absolute;
left:'+(((pos-750))/2+293)+'; top:19; z-index:4;
visibility:hidden;">');

and pos is finally tha browser window size. So the layer outputs in
the right place in the browser window.
Jul 23 '05 #3
Justin Koivisto wrote:
mbasil7 wrote:
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'>
The `language' attribute is deprecated, the `type' attribute is mandatory
in HTML 4.
<!--
Commenting out scripts is a deprecated error-prone approach that is based
on a misconception.
var a=(screen.width-750)/2;
Using properties of `screen' as a means to access the display resolution
does not work. Learn to design documents that are usable with any probable
display resolution.
function getvariable(val) {
var dummy = eval(val);
document.write(dummy);
eval() is evil[tm] and not at all required here.
}
// -->
See above.
</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>
No comment.
[...]
The only way you will be able to use a Javascript-generated variable in
PHP is by sending another request to the server. By the time the
javascript executes, php has already done it's job. To do this, you may
want to do something like:


*Very* like.
<html>
<head>
This is not Valid HTML. <http://validator.w3.org/>
<script type="text/javascript">
<!--
getJsValue = function (){
var a=(screen.width-750)/2;
See above.
var url='<?php echo $_SERVER['PHP_SELF'] ?>?a='+a;
document.location.href=url;
`document.location' is deprecated long since. Use `window.location' or
just `location'. Furthermore,

location = location;

or

location.reload(true);

is sufficient, PHP is not at all needed for this part.
}
-->
This is a syntax error, the `--' is the decrement operator and it requires
a reference as operand. That is why the OP commented it out with `//'.
</script>
</head>
<body <?php if(!isset($_GET['a'])) echo 'onload="getJsValue(); return
true;"'; ?>>
<?php
if(isset($_GET['a'])){
echo 'Javascript value: ',$_GET['a'];
}else{
echo 'refreshing';
}
?>
Learn to understand where PHP is to be used and where it is not.
Even if the above would work, it should be

<body<?php
if (!isset($_GET['a']))
{
?> onload="getJsValue(); return true;"<?php
}
?>>
<?php
if (isset($_GET['a'])) {
?>
Javascript value: <?php
echo $_GET['a'];
}
else
{
?>
refreshing
<?php
}
?>
HTH


Hardly.
PointedEars
Jul 23 '05 #4

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

Similar topics

1
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.