By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,504 Members | 1,190 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,504 IT Pros & Developers. It's quick & easy.

Bring variables from php to JAVASCRIPT

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
If you wanted to save a few keystrokes you could
var foo = "<?=$foo; ?>"

Jul 23 '05 #5

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.