Supa Comix 0 Report post Posted October 23, 2007 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? <?php$color = "green";?><script language="JavaScript">BackColor= "<?php echo($color);?>";</script> Share this post Link to post Share on other sites
ethergeek 0 Report post Posted October 23, 2007 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. Share this post Link to post Share on other sites
vujsa 0 Report post Posted October 23, 2007 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: <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 Share this post Link to post Share on other sites
Supa Comix 0 Report post Posted October 23, 2007 Okay groovy thanks people. Thats exactly what i wanted. Share this post Link to post Share on other sites
TavoxPeru 0 Report post Posted October 24, 2007 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? <?php$color = "green";?><script language="JavaScript">BackColor= "<?php echo($color);?>";</script> Yes, it works fine. I do this kind of things every time.Best regards, Share this post Link to post Share on other sites
iGuest 3 Report post Posted November 22, 2007 Yes, it works fine. I do this kind of things every time.Best regards, Heres what i always do:<?php$var="stored_variable";?><script type="Javascript"><!--var string="<?php echo $var;?>";//--></script> Share this post Link to post Share on other sites
iGuest 3 Report post Posted January 21, 2010 That doenSend Php Variable To JavascriptI'm really cracking up this morning. My computer decided that everything I'll try to do, as simple as: <?php$color = "green";?> <script type="text/javascript">Var BackColor = '<?php echo $color;?>';Document.Write(BackColor);</script> would not work. Of course ! Because it would be soooo easy. Please help me !! -reply by Diane Share this post Link to post Share on other sites
8ennett 0 Report post Posted January 29, 2010 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. That's not entirely true. You can insert php values in to javascript prior to the page being loaded which is exactly what this example is doing. The only way to do it the other way around would be to use ajax to call a seperate php document after the page has loaded. Share this post Link to post Share on other sites
8ennett 0 Report post Posted January 29, 2010 That doen Send Php Variable To Javascript I'm really cracking up this morning. My computer decided that everything I'll try to do, as simple as: <?php $color = "green"; ?> <script type="text/javascript"> Var BackColor = '<?php echo $color;?>'; Document.Write(BackColor); </script> would not work. Of course ! Because it would be soooo easy. Please help me !! -reply by Diane You need to make the document, write and var lowercase words! <script type="text/javascript"> var BackColor = ""; document.write(BackColor); </script> Share this post Link to post Share on other sites