Passing variables between JavaScript and PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Passing variables between JavaScript and PHP

Post by Chris Corbyn »

Hi,

I need to pass one or two variables from my PHP into a JavaScript on the same page.

I've had a read in the PHP manual and it does mention that you can pass variables from JavaScript into PHP but I cannot find a way to do the opposite.

Basically I'm trying to make a form which has a JavaScript function to change what is displayed when a user changes selections in the form. I need to use PHP to look up a filesize based on user input and then JavaScript to display what it finds (without using a submit button).

Thanks :-)
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

User avatar
MarK (CZ)
Forum Contributor
Posts: 239
Joined: Tue Apr 13, 2004 12:51 am
Location: Prague (CZ) / Vienna (A)
Contact:

Re: Passing variables between JavaScript and PHP

Post by MarK (CZ) »

d11wtq wrote:Hi,

I need to pass one or two variables from my PHP into a JavaScript on the same page.
You mean it just like this?:

Code: Select all

<SCRIPT LANGUAGE="JavaScript">
  var myVar = <?= $phpVar ?>;
</SCRIPT>
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

in the form you can use onChange or onClick or whatever to redirect to self with vars in the URL
Post Reply