passing PHP varibales to Javascript...

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
eyewoo
New php-forum User
New php-forum User
Posts: 71
Joined: Sun Mar 09, 2003 10:29 am
Location: Pennsylvania, USA
Contact:

Sat Jun 14, 2003 1:33 pm

... is there a way?

I thought this would work:

Code: Select all


<script langauge="javascript">
myJVab = <?=$MyPHPVab?>;
document.write(myJVab + ' has been passed to Javascript from PHP<hr>');
</script>



...but it doesn't... Apparently the <?= ?> do not have effect within the <script></script> tags... :cry:
Philip Williams
Image

eyewoo
New php-forum User
New php-forum User
Posts: 71
Joined: Sun Mar 09, 2003 10:29 am
Location: Pennsylvania, USA
Contact:

Sat Jun 14, 2003 1:50 pm

Got it!

This on the other hand does work...

Code: Select all

<?
$myPHPVab='OneTwoThreeFour';
echo '
<script langauge="javascript">;
   var myJVab = "'.$myPHPVab.'";
   document.write(myJVab + " has been passed to Javascript from PHP<hr>");
</script>
';
?>


The <script></script> tags have to also be encased within the PHP tags... :D
Philip Williams
Image

bonkers
New php-forum User
New php-forum User
Posts: 45
Joined: Wed Mar 12, 2003 7:17 pm

Sat Jun 14, 2003 10:12 pm

Forgive me if I'm wrong cuz I'm a n00b at PHP but I thought that since the file is parsed for <??> tags that any other tags don't matter when it's in your HTML. So <?=$variable?> should work. Try <? echo $variable; ?>...might work ya neva know.

eyewoo
New php-forum User
New php-forum User
Posts: 71
Joined: Sun Mar 09, 2003 10:29 am
Location: Pennsylvania, USA
Contact:

Sun Jun 15, 2003 6:07 am

Nope... from my experiments, the HTML <script></script> tag has to be part of what is encased in the PHP <? ?> tags, i.e. the <script></script> tags have to be echo'd before the PHP variables can be inserted. At least that is the way it works using the Apache mod PHP. Perhaps it's different with the regular PHP... I've noticed other difference in the two versions of PHP.
Philip Williams
Image

User avatar
WiZARD
Moderator
Moderator
Posts: 1240
Joined: Thu Jun 20, 2002 10:14 pm
Location: Ukraine, Crimea, Simferopol
Contact:

Sun Jun 15, 2003 11:37 pm

he-he-he :D :lol: :D
not straight, so a roundabout way
"Sex,Drugs and Rock&Roll " replaced at "Sucks,Bugs and Plug&Play";
Image

Post Reply