Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Computer Forums > Programmers Lounge > Programming Discussions » passing php variables to javascript - need help
Reply
Old 11-01-2009, 02:36 PM   #1 (permalink)
 
Newb Techie

Join Date: Oct 2009

Posts: 9

reedjasonf is on a distinguished road

Default passing php variables to javascript - need help

I have 2 different files: vars.php and functions.js.

Inside my vars.php I have declared a few variables:
example:
$var1 = "This is a string";
$var2 = 50;
$var['array'] = "This is an array";

I'd like to pass these variables to my functions.js so that if I ever need to change the variables I only need to do that in the php file containing all my variables.

I've looked at other forums and they say I should be able to do this:

var jsvar = "<?php echo $var1;?>";
... but when I do this jsvar becomes literally "<?php echo $var1;?>" instead of "This is a string" like I want it to. Anyone know what my problem is?
reedjasonf is offline   Reply With Quote
Old 11-01-2009, 02:38 PM   #2 (permalink)
 
Newb Techie

Join Date: Oct 2009

Posts: 9

reedjasonf is on a distinguished road

Default Re: passing php variables to javascript - need help

Oh, I forgot to say that I've already used <?php include('vars.php'); ?> in my javascript file.
reedjasonf is offline   Reply With Quote
Old 11-04-2009, 04:05 AM   #3 (permalink)
 

Join Date: Jul 2005

Location: England

Posts: 2,159

kmote has a spectacular aura aboutkmote has a spectacular aura about

Default Re: passing php variables to javascript - need help

You cannot directly share variables between server and client like this. Your method won't work because even if php was running on the client system through the browser it wouldn't be, for lack of a better word, the same "instance" that is running on the server
__________________
MSI P43 Neo|Enermax Pro82+ 425W|E5200|silent 8500GT|250GB Samsung spinpoint F1|Samsung SATA DVD RW|4GB Corsair|Antec SOLO|openSUSE11


There are in order of increasing severity: lies, darn lies, statistics, and computer benchmarks. - diskinfo man page
kmote is offline   Reply With Quote
Old 11-05-2009, 04:53 PM   #4 (permalink)
office politics's Avatar
 
It's all just 1s and 0s

Join Date: Jan 2004

Location: in the lab

Posts: 4,410

office politics will become famous soon enough

Default Re: passing php variables to javascript - need help

this person should be able to to echo out strings in the php script for use in the javascript code.

data could then be "posted" back to the script via a webform.


it seems their php isnt parsing correctly.
office politics is offline   Reply With Quote
Old 11-05-2009, 06:23 PM   #5 (permalink)
CrazeD's Avatar
 
Wizard Techie

Join Date: Feb 2006

Location: Maine

Posts: 3,683

CrazeD will become famous soon enough

Send a message via AIM to CrazeD Send a message via MSN to CrazeD
Default Re: passing php variables to javascript - need help

Code:
<?php

$var1 = "This is a string";
$var2 = 50;
$var['array'] = "This is an array";

echo '<script type="text/javascript">var jsvar=' . $var1 . '</script>';

?>
This should work.
__________________

Need website help? PM me!
CrazeD is offline   Reply With Quote
Old 11-05-2009, 07:50 PM   #6 (permalink)
 

Join Date: Jul 2005

Location: England

Posts: 2,159

kmote has a spectacular aura aboutkmote has a spectacular aura about

Default Re: passing php variables to javascript - need help

Quote:
Originally Posted by office politics View Post
this person should be able to to echo out strings in the php script for use in the javascript code.

data could then be "posted" back to the script via a webform.


it seems their php isnt parsing correctly.
You can echo js out of a php script, yes, but look at the code he has posted. He is putting php into a js string variable... the js will be able to use that string variable on the client but the client won't then be able to "run" the php as if it were on the server.
__________________
MSI P43 Neo|Enermax Pro82+ 425W|E5200|silent 8500GT|250GB Samsung spinpoint F1|Samsung SATA DVD RW|4GB Corsair|Antec SOLO|openSUSE11


There are in order of increasing severity: lies, darn lies, statistics, and computer benchmarks. - diskinfo man page
kmote is offline   Reply With Quote
Old 11-06-2009, 02:10 PM   #7 (permalink)
office politics's Avatar
 
It's all just 1s and 0s

Join Date: Jan 2004

Location: in the lab

Posts: 4,410

office politics will become famous soon enough

Default Re: passing php variables to javascript - need help

in my mind, they are trying to duplicate a variable in php to a variable in javascript.

I agree that the php code will not run client side.

the jsvar line should be in the php script
office politics is offline   Reply With Quote
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
php javascript window.open iframe problem Larry Programming Discussions 5 10-29-2009 01:18 AM
PHP - what it does and what it doesn’t Osiris Programming Discussions 1 02-16-2009 04:09 PM
PHP: How to modify a downloaded JavaScript code and partly change the code within it? LincolnX Programming Discussions 4 01-25-2008 04:25 PM
What Is Harder, JavaScript Or PHP? Torner Programming Discussions 8 01-16-2008 08:11 AM
Need PHP and JavaScript Form Validation Scripts aetherh4cker Programming Discussions 2 01-06-2008 04:16 PM