Welcome to Dream.In.Code
Getting Help is Easy!

Join 132,640 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,085 people online right now. Registration is fast and FREE... Join Now!




Syntax For Using Php Value In Javascript

 
Reply to this topicStart new topic

Syntax For Using Php Value In Javascript, php and javascript

graeder
post 8 Dec, 2003 - 12:59 PM
Post #1


New D.I.C Head

*
Joined: 17 Dec, 2002
Posts: 22

I'm new to php, but can anyone give me the proper syntax to initialize a javascript variable with a php variable value?
User is offlineProfile CardPM

Go to the top of the page

skyhawk133
post 8 Dec, 2003 - 03:25 PM
Post #2


Head DIC Head

Group Icon
Joined: 17 Mar, 2001
Posts: 14,846



Thanked 45 times

Dream Kudos: 1650

Expert In: Web Development

My Contributions


What is in your javascript that you need? What variable? How is it being passed, what was it from originally?

If you can answer these I can probably give you the best way to do this.
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 8 Dec, 2003 - 03:41 PM
Post #3


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,176



Thanked 33 times

Dream Kudos: 25
My Contributions


QUOTE(graeder @ Dec 8 2003, 03:59 PM)
I'm new to php, but can anyone give me the proper syntax to initialize a javascript variable with a php variable value?

Hmm, I'm not sure about proper syntax, but here's the way I do it (and the way I do it in ASP).

CODE

<? $vartopass = "Hello"; ?>
<script = "javascript">
var strnewVar = '<?=$vartopass?>';
alert(strnewVar);
</script>

This may not be the proper way, but it works for me.

HTH! smile.gif
User is offlineProfile CardPM

Go to the top of the page

gneato
post 8 Dec, 2003 - 04:28 PM
Post #4


<title>Untitled Document</title>

*****
Joined: 3 Sep, 2001
Posts: 1,311

wink2.gif

Posted then realized I misread...

This post has been edited by gneato: 8 Dec, 2003 - 04:30 PM
User is offlineProfile CardPM

Go to the top of the page

cyberscribe
post 8 Dec, 2003 - 06:15 PM
Post #5


humble.genius

Group Icon
Joined: 5 May, 2002
Posts: 1,062



Thanked 2 times

Dream Kudos: 154
My Contributions


QUOTE(Amadeus @ Dec 8 2003, 04:41 PM)
This may not be the proper way, but it works for me.

That's the ASP way. In PHP, like ASP, you can print any variables to output, including HTML output, including inside a JavaScript tag like this:

CODE

<?PHP
print "<script language=\"JavaScript1.2\">\nfoo=$foo;\n</script>";
?>

(results at http://www.peakepro.com/workbench/foo.php?foo=3 )

or this:

CODE

<script language="JavaScript1.2">
foo=<?PHP print $foo; ?>;
</script>

(results at http://www.peakepro.com/workbench/foo2.php?foo=3 )

Either will work. Be wary of mixing HTML and PHP in large applications; it can create maintenance headaches.

Cheers,
RP
User is offlineProfile CardPM

Go to the top of the page

gneato
post 8 Dec, 2003 - 11:26 PM
Post #6


<title>Untitled Document</title>

*****
Joined: 3 Sep, 2001
Posts: 1,311

QUOTE(cyberscribe @ Dec 8 2003, 07:15 PM)
<?PHP print $foo; ?>

<?php print $foo; ?> == <?=$foo?>

in more recent versions of PHP...

This post has been edited by gneato: 8 Dec, 2003 - 11:27 PM
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 9 Dec, 2003 - 05:53 PM
Post #7


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,176



Thanked 33 times

Dream Kudos: 25
My Contributions


QUOTE(cyberscribe @ Dec 8 2003, 09:15 PM)

Either will work. Be wary of mixing HTML and PHP in large applications; it can create maintenance headaches.

Cheers,
RP

As will the method I described. I'll agree with cyberscribe's caveat concerning the mixing of both, however. It can get nasty to debug on large apps.
User is offlineProfile CardPM

Go to the top of the page

cyberscribe
post 9 Dec, 2003 - 07:06 PM
Post #8


humble.genius

Group Icon
Joined: 5 May, 2002
Posts: 1,062



Thanked 2 times

Dream Kudos: 154
My Contributions


QUOTE(gneato @ Dec 9 2003, 12:26 AM)
<?php print $foo; ?> == <?=$foo?>

in more recent versions of PHP...

Yikes! PHP is turning into Perl. blink.gif

Couldn't find the versioning, but found this along the way which is worth noting:

http://www.faqts.com/knowledge_base/view.p...ml/aid/1/fid/40
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 04:31AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month