473,395 Members | 1,986 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,395 software developers and data experts.

Javascript and PHP ?

Jay
Hi everybody !!

I am just wondering if Javascript and PHP can interact with each other
?

Let's say i have a variable in Javascript ( call it "var x = 6;")
Is there anyway that we can use "x" value (which is 6) in PHP code?
(let's say I want to print "x" out in PHP script)

In general, can Javascript variables interact with PHP code and vice
versa ?

Thank you in advance !!

PS: My initial thought is this: "It is impossible to do !" Prove me
wrong ==> :)
Jul 17 '05 #1
6 2052
Jay wrote:
I am just wondering if Javascript and PHP can interact with each other

Let's say i have a variable in Javascript ( call it "var x = 6;")
Is there anyway that we can use "x" value (which is 6) in PHP code?
(let's say I want to print "x" out in PHP script)

In general, can Javascript variables interact with PHP code and vice
versa ?


Javascript is a client-side technology. PHP is a server-sided technology.
You can write out Javascript using PHP and you can post stuff in web forms
and using GET URLs back to the server which can then be processed by PHP.

In your example, you would need to assign the "x" variable to a hidden form
value so it's actually posted back to the server, which PHP would then be
able to use.

There is no way for PHP to access Javascript variables, other than them
being submitted in a form, modifying the form's post property or
redirecting to another page in Javascript and passing the value as part of
the URL.

--
Chris Hope
The Electric Toolbox - http://www.electrictoolbox.com/
Jul 17 '05 #2
"Chris Hope" <ch***@electrictoolbox.com> wrote in message
news:10************@news.athenanews.com...
Jay wrote:
I am just wondering if Javascript and PHP can interact with each other

Let's say i have a variable in Javascript ( call it "var x = 6;")
Is there anyway that we can use "x" value (which is 6) in PHP code?
(let's say I want to print "x" out in PHP script)

In general, can Javascript variables interact with PHP code and vice
versa ?
Javascript is a client-side technology. PHP is a server-sided technology.
You can write out Javascript using PHP and you can post stuff in web forms
and using GET URLs back to the server which can then be processed by PHP.

In your example, you would need to assign the "x" variable to a hidden

form value so it's actually posted back to the server, which PHP would then be
able to use.

There is no way for PHP to access Javascript variables, other than them
being submitted in a form, modifying the form's post property or
redirecting to another page in Javascript and passing the value as part of
the URL.

--
Chris Hope
The Electric Toolbox - http://www.electrictoolbox.com/


Then to follow up Jay's question, how do people get information about screen
resolution, operating system, etc. on visitors to their websites? Everyone
says it must be done through javascript, but javascript doesn't insert
information into databases. And if PHP can't access javascript variables,
then how can you get someone's screen resolution and put it into a database
without visitors submitting a form?

- JP
Jul 17 '05 #3
kingofkolt wrote:
Then to follow up Jay's question, how do people get information about
screen resolution, operating system, etc. on visitors to their websites?
Everyone says it must be done through javascript, but javascript doesn't
insert information into databases. And if PHP can't access javascript
variables, then how can you get someone's screen resolution and put it
into a database without visitors submitting a form?


That was one thing I forgot. You can use document.write() to write out stuff
into the page as well. You could use this to write out a 1x1 pixel "image"
which is actually a PHP script that passes in the screen width and height,
image depth etc. Note that it still needs to be part of the url eg
image.php?width=1024&height=768 (obviously you would construct this url
using Javascript and the appropriate variables that contain the width and
height and all the cross browser code you need to make it work correctly).

For operating system and browser information it's passed as part of the
user-agent string from the client to the server and is stored in the
$_SERVER and $_ENV super globals; no need to use Javascript for that.

Chris

Jul 17 '05 #4
Chris Hope wrote:
or
redirecting to another page in Javascript and passing the value as part of
the URL.


Interesting. I hadn't thought of that, yet. Is creating a
tiny/invisible new window from Javascript, and submitting form pages
through it, a valid technique for setting up a vague kind of handshaking
and client-server transfer? I haven't thought through the limitations
and capabilities, but it might come in handy sometime.

--
--
Lynn Wallace http://www.xmission.com/~lawall
"We should not march into Baghdad. ... Assigning young soldiers to
a fruitless hunt for a securely entrenched dictator and condemning
them to fight in what would be an unwinnable urban guerilla war, it
could only plunge that part of the world into ever greater
instability." George Bush Sr. in his 1998 book "A World Transformed"

Jul 17 '05 #5
"Chris Hope" <ch***@electrictoolbox.com> wrote in message
news:10*************@news.athenanews.com...
kingofkolt wrote:
Then to follow up Jay's question, how do people get information about
screen resolution, operating system, etc. on visitors to their websites?
Everyone says it must be done through javascript, but javascript doesn't
insert information into databases. And if PHP can't access javascript
variables, then how can you get someone's screen resolution and put it
into a database without visitors submitting a form?
That was one thing I forgot. You can use document.write() to write out

stuff into the page as well. You could use this to write out a 1x1 pixel "image"
which is actually a PHP script that passes in the screen width and height,
image depth etc. Note that it still needs to be part of the url eg
image.php?width=1024&height=768 (obviously you would construct this url
using Javascript and the appropriate variables that contain the width and
height and all the cross browser code you need to make it work correctly).

For operating system and browser information it's passed as part of the
user-agent string from the client to the server and is stored in the
$_SERVER and $_ENV super globals; no need to use Javascript for that.

Chris


Ok thanks. I had always heard and seen people collecting screen res. thru
javascript but I never knew how.

- JP
Jul 17 '05 #6
In article <56**************************@posting.google.com >, ar********@yahoo.com (Jay) wrote:
Hi everybody !!

I am just wondering if Javascript and PHP can interact with each other?


I had the same problem with a feature I wanted to do with passing a
variable to a PHP script on a javascript popup and the PHP code to continue
executing the code in the popup depending on the variables passed to it.

I could not work out a solution, but after hours of searching (sorry didn't
bookmark the URL), I did find a solution. Here's the code I have.

Suppose that you want to call a popup, like I will. The script you want to
call will call a Javascript popup and pass the variables from the initial
click to the Javascript, which in turn will go to PHP to execute.

So suppose you have a page which will call a popup like this:

<script src="js/pics.js" type="text/javascript">
<!--
//-->
</script>

<body>
<a href="#" onclick="go_open('doit.php?&gallery=01&picID=01'); ">
</body>
This URL will go and run the javascript "pics.js" script which contains
something like:

<!--
function go_open(url)
{
window.
open(url,"Picture","width=50,height=50,toolbar=no, scrollbars=no,
status=yes");
}
//-->
Now, at the VERY beginning of the "doit.php" page - you have to write the
PHP / Javascript page like the following (before the <html> section).

<?PHP
// Import the variables from Javascript window.open() method to PHP.

$G-ID = $_GET['gallery'];
$P-ID = $_GET['picID'];

?>
//-->
</script>

<head><title>Popup</title></head>
<body>

Normal HTML page here mixed with PHP to make use of the vaiables.
Now later on in the page, you can then call the variables for $G-ID and
$P-ID as you have imported them using Javascript and PHP.

Complicated, but it does work.

Dariusz
Jul 17 '05 #7

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

Similar topics

0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...
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...

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.