7 Replies - 19778 Views - Last Post: 09 December 2003 - 08:06 PM

#1 graeder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 17-December 02

Syntax For Using Php Value In Javascript

Posted 08 December 2003 - 01: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?
Is This A Good Question/Topic? 0
  • +

Replies To: Syntax For Using Php Value In Javascript

#2 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1875
  • View blog
  • Posts: 20,283
  • Joined: 17-March 01

Re: Syntax For Using Php Value In Javascript

Posted 08 December 2003 - 04:25 PM

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.
Was This Post Helpful? 0
  • +
  • -

#3 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Syntax For Using Php Value In Javascript

Posted 08 December 2003 - 04:41 PM

graeder, on Dec 8 2003, 03:59 PM, said:

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).

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


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

HTH! :)
Was This Post Helpful? 0
  • +
  • -

#4 gneato  Icon User is offline

  • <title>Untitled Document</title>

Reputation: 0
  • View blog
  • Posts: 1,311
  • Joined: 03-September 01

Re: Syntax For Using Php Value In Javascript

Posted 08 December 2003 - 05:28 PM

;)

Posted then realized I misread...

This post has been edited by gneato: 08 December 2003 - 05:30 PM

Was This Post Helpful? 0
  • +
  • -

#5 cyberscribe  Icon User is offline

  • humble.genius
  • member icon

Reputation: 10
  • View blog
  • Posts: 1,062
  • Joined: 05-May 02

Re: Syntax For Using Php Value In Javascript

Posted 08 December 2003 - 07:15 PM

Amadeus, on Dec 8 2003, 04:41 PM, said:

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:

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


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

or this:

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


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

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

Cheers,
RP
Was This Post Helpful? 0
  • +
  • -

#6 gneato  Icon User is offline

  • <title>Untitled Document</title>

Reputation: 0
  • View blog
  • Posts: 1,311
  • Joined: 03-September 01

Re: Syntax For Using Php Value In Javascript

Posted 09 December 2003 - 12:26 AM

cyberscribe, on Dec 8 2003, 07:15 PM, said:

<?PHP print $foo; ?>

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

in more recent versions of PHP...

This post has been edited by gneato: 09 December 2003 - 12:27 AM

Was This Post Helpful? 0
  • +
  • -

#7 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Syntax For Using Php Value In Javascript

Posted 09 December 2003 - 06:53 PM

cyberscribe, on Dec 8 2003, 09:15 PM, said:

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.
Was This Post Helpful? 0
  • +
  • -

#8 cyberscribe  Icon User is offline

  • humble.genius
  • member icon

Reputation: 10
  • View blog
  • Posts: 1,062
  • Joined: 05-May 02

Re: Syntax For Using Php Value In Javascript

Posted 09 December 2003 - 08:06 PM

gneato, on Dec 9 2003, 12:26 AM, said:

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

in more recent versions of PHP...

Yikes! PHP is turning into Perl. :blink:

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

http://www.faqts.com...ml/aid/1/fid/40
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1