Jump to content
xisto Community
Supa Comix

Send Php Variable To Javascript

Recommended Posts

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

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

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

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

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
That doenSend 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

Share this post


Link to post
Share on other sites

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

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.