473,503 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Bring variables from php to JAVASCRIPT

Hello,

Does someone know how can we bring variables from a PHP script to a
Javascript one ?
I have to build a Javascript function that needs variables generated by a
PHP script but i do not know the way to retrieve them into Javascript.

Many tanks in advance for your help
Rdgs

Steph
Jul 23 '05 #1
5 1487
Steph wrote:
Does someone know how can we bring variables from a PHP script to a
Javascript one ?
I have to build a Javascript function that needs variables generated by a
PHP script but i do not know the way to retrieve them into Javascript.


All the PHP runs on the server, then all the JavaScript runs on the client.
For PHP to send information to JavaScript it has to output JavaScript code.

<script type="text/javascript">
var foo = "<?php echo $foo; ?>";
</script>

For JavaScript to send information to PHP, it has to get the browser to make
a new HTTP request.

document.location = "http://www.example.com/myScript.php?foo=" + foo;

--
David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>
Home is where the ~/.bashrc is
Jul 23 '05 #2
"David Dorward" <do*****@yahoo.com> a écrit dans le message de news:
cu*******************@news.demon.co.uk...
Steph wrote:
Does someone know how can we bring variables from a PHP script to a
Javascript one ?
I have to build a Javascript function that needs variables generated by a
PHP script but i do not know the way to retrieve them into Javascript.


All the PHP runs on the server, then all the JavaScript runs on the
client.
For PHP to send information to JavaScript it has to output JavaScript
code.

<script type="text/javascript">
var foo = "<?php echo $foo; ?>";
</script>

For JavaScript to send information to PHP, it has to get the browser to
make
a new HTTP request.

document.location = "http://www.example.com/myScript.php?foo=" + foo;

--
David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>
Home is where the ~/.bashrc is


I've made a test and it works.
Thank you very much !
Jul 23 '05 #3
You might find this project handy for getting Javascript to talk to PHP
over XmlHttpRequest: http://jpspan.sourceforge.net/wiki/doku.php

Jul 23 '05 #4
If you wanted to save a few keystrokes you could
var foo = "<?=$foo; ?>"

Jul 23 '05 #5
Peter Stickney wrote:
If you wanted to save a few keystrokes you could
var foo = "<?=$foo; ?>"


Save a few keystroks compared to what? Please quote some context!

That depends on the PHP short tags option being enabled.

--
David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>
Home is where the ~/.bashrc is
Jul 23 '05 #6

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

Similar topics

16
11178
by: Craig L | last post by:
I use ASP to obtain data from a database and I have a piece of javascript code I use on my website. I want to use the database variables in my javascript. I'm not very familiar with javascript, but...
2
4240
by: Craig L | last post by:
This is a solution followup to a request for help posted on 11/04/03 at 12:51pm titled "Using ASP Variables". Here is how you can make ASP variables available for use in Javascript code. I hope you...
8
2509
by: lawrence | last post by:
I'm learning Javascript. I downloaded a script for study. Please tell me how the variable "loop" can have scope in the first function when it is altered in the second function? It is not defined...
1
1350
by: Taffy Collins | last post by:
I have an ASP page that contains an A HREF tag. This will load a new page and carry some variables to the new page. What I want is that, whenever a person click the item,the prompt box comes up and...
6
5476
by: Paolo Pignatelli | last post by:
I have an aspx code behind page that goes something like this in the HTML view: <asp:HyperLink id=HyperLink1 runat="server" NavigateUrl='<%#"mailto:" &amp;...
7
8034
by: jab3 | last post by:
Does JavaScript have "static" variables. That is, as in C (or local in Perl)? How can I keep a variable in a JavaScript function that doesn't change from call to call? It may not make sense in...
8
2366
by: mantrid | last post by:
Hello I have the following code, where clicking yh1r is supposed to move h1 10px down and update the value of yh1 by 20 each time its clicked. what the code actually does is NOT move h1 and...
3
2054
by: Vic Spainhower | last post by:
Hello, I have an HTML table that is being constructed from a MySQL table and displays a form that includes a check box on 1 of the fields on the form for each record. I have included in this PHP...
0
7274
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,...
1
6984
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
7453
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
5576
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,...
1
5005
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...
0
3162
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...
0
3151
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1507
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
377
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...

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.