PDA

View Full Version : PHP to JavaScript Variables



tech_support
09-27-2006, 08:18 AM
Hi,

I was wondering if you can transfer the PHP variable to JavaScript?

Like:



<?php

$name = $_GET['name'];

?>


then get the $name variable into JavaScript like...



var name = (name here)


Thanks,

Peter

djr33
09-27-2006, 08:23 AM
Sure.
var name = <?php echo $_GET['name']; ?>

tech_support
09-27-2006, 08:25 AM
Oh. Thanks!

Never thought it would be THAT simple.

djr33
09-27-2006, 08:26 AM
Yep.
You can do anything like that... you're just echoing into the html source, and since js is part of the html source, that's all there is to it.

Twey
09-27-2006, 05:35 PM
djr33 missed out the quotes enclosing the variable, however.

djr33
09-29-2006, 05:25 AM
Yep. This is the php forum, not the JS forum :p
Silly me. Heh.

Th3_Designer
10-16-2006, 08:18 AM
Or in the PHP file:

$name = $GET['name'];

echo "<script language=\"JavaScript\" type=\"JavaScript/text\">
var name = '$name'; </script>";

and in your HTML,

<html>
<title> Test </title>
<body>
<script language="javascript">
document.write(top.name);
</script>
</body>
</html>

Twey
10-16-2006, 04:10 PM
echo "<script language=\"JavaScript\" type=\"JavaScript/text\">
var name = '$name'; </script>";language is deprecated, and the commonly-implemented Javascript MIME type is text/javascript, not JavaScript/text.
document.write(top.name);There is no guarantee that window.top === window.