passing PHP varibales to Javascript...

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

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

passing PHP varibales to Javascript...

Postby eyewoo » 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:

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

ah-ha!!!!!

Postby eyewoo » 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

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

Postby bonkers » 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:

Postby eyewoo » 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.

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

Postby WiZARD » Sun Jun 15, 2003 11:37 pm

he-he-he :D :lol: :D
not straight, so a roundabout way


Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 1 guest