Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> Send Php Variable To Javascript
Supa Comix
post Oct 23 2007, 02:01 PM
Post #1


Newbie [ Level 2 ]
Group Icon

Group: Members
Posts: 24
Joined: 9-August 07
Member No.: 23,954



Right i had a look across the internet as well as a search on here but you cannot search for anything less than 3 characters. But this is a really quick question.

Would the following code allow me to send a php variable to a javascript?

CODE
<?php
$color = "green";
?>
<script language="JavaScript">
BackColor= "<?php echo($color);?>";

</script>
Go to the top of the page
 
+Quote Post
ethergeek
post Oct 23 2007, 03:02 PM
Post #2


Premium Member
Group Icon

Group: [HOSTED]
Posts: 393
Joined: 9-March 07
From: Tucson, AZ
Member No.: 20,794



I don't think this is possible...php and javascript are handled by two different preprocessors, one of them is php, the other is handled by the servlet engine. There may be a way to do it specifically using Tomcat and php installed into Tomcat, try the documentation...but as far as doing it with apache and the tomcat redirector...you may be out of luck.
Go to the top of the page
 
+Quote Post
vujsa
post Oct 23 2007, 03:27 PM
Post #3


Absolute Newbie
Group Icon

Group: Admin
Posts: 888
Joined: 20-February 05
From: Indianapolis, Indiana, USA (Midwest)
Member No.: 2,714
myCENTs:35.43



In short, yes!

there is no reason for this not to work. In fact, you could have PHP dynamically generate an entire JavaScript on the fly if you wanted. This is because PHP is parsed on the server so the browser only sees the output and JavaScript is parsed in the browser so what ever the browser sees, JavaScript can do...

Here is what your PHP code would output:
CODE
<script language="JavaScript">
BackColor= "green";

</script>


Now if you wanted JavaScript to send variables back to PHP, there is some issues but depending on what you want, this could be done using either the POST or GET method of variable passing but usually requires a new page to be loaded. It is possible with AJAX to pass variable to and from the server without the need for a new page load but that is another discussion.

vujsa
Go to the top of the page
 
+Quote Post
Supa Comix
post Oct 23 2007, 04:21 PM
Post #4


Newbie [ Level 2 ]
Group Icon

Group: Members
Posts: 24
Joined: 9-August 07
Member No.: 23,954



Okay groovy thanks people. Thats exactly what i wanted.
Go to the top of the page
 
+Quote Post
TavoxPeru
post Oct 24 2007, 04:04 AM
Post #5


Super Member
Group Icon

Group: [HOSTED]
Posts: 805
Joined: 8-April 06
From: Lima - Peru
Member No.: 12,579
myCENTs:46.87



QUOTE(Supa Comix @ Oct 23 2007, 09:01 AM) *
Right i had a look across the internet as well as a search on here but you cannot search for anything less than 3 characters. But this is a really quick question.

Would the following code allow me to send a php variable to a javascript?

CODE
<?php
$color = "green";
?>
&lt;script language="JavaScript">
BackColor= "<?php echo($color);?>";

</script>

Yes, it works fine. I do this kind of things every time.

Best regards,
Go to the top of the page
 
+Quote Post
Aldo
post Nov 22 2007, 09:59 AM
Post #6


Newbie [ Level 1 ]
Group Icon

Group: Banned
Posts: 7
Joined: 22-November 07
Member No.: 26,356



QUOTE(TavoxPeru @ Oct 24 2007, 08:04 AM) *
Yes, it works fine. I do this kind of things every time.

Best regards,



Heres what i always do:
CODE
<?php
$var="stored_variable";
?>
<script type="Javascript">
<!--
var string="<?php echo $var;?>";
//-->
</script>
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Help: $_post Variable For Options From Select Types?(6)
  2. How To Reset The Server Variable Php_auth_user(9)
  3. Need Help With Javascript In Php Urgently.(4)
  4. Variable From Line Further Then Current Line?(13)
  5. Quickly Create Form Variables(5)
  6. Php Any Variable In String.(1)
  7. Unexpected Error(2)
  8. Extplorer(7)
  9. Permanent Variable(7)


 



- Lo-Fi Version Time is now: 5th December 2008 - 12:30 PM