How do we insert PHP code into JavaScript code?

  • Home How do we insert PHP code into JavaScript code?
How do we insert PHP code into JavaScript code?

How do we insert PHP code into JavaScript code?

July 5, 2009

Question:
How do we insert php code into javascript code?

Answer:
PHP is server-side code which means that the code is executed on the web server. The pages are dynamically created and sent to the user's browser.

JavaScript is a client-side code which means that it runs on the user's computer after the page has been sent from the server.

It is possible to combine PHP commands within JavaScript commands.

You would mainly use this in setting up starting variables for your JavaScript.

Example:
<script language='JavaScript'>
<!--
var BoxColor = <?php echo $color;?>;
…etc
-->
</script>

Another option is that you may consider using Ajax which allows you to dynamically call server-side code from JavaScript.

About the Author
Rich Agnew is an author for The Computer Geek Custom Web Page Design. The Computer Geek is a web design company that prides itself in professional service at a fraction of the cost. The Computer Geek specializes in Custom Web Design, PHP & MySql and Ecommerce.

To Make a Request For Further Information

5K

Happy Clients

12,800+

Cups Of Coffee

5K

Finished Projects

72+

Awards
TESTIMONIALS

What Our Clients
Are Saying About Us

Get a
Free Consultation


LATEST ARTICLES

See Our Latest
Blog Posts

The Role of User Experience in E-Commerce Success
November 20, 2024

The Role of User Experien

Top Strategies for Improving Organic Search Rankings
November 19, 2024

Top Strategies for Improv

The Benefits of Using Video Content for Marketing
November 18, 2024

The Benefits of Using Vid

Intuit Mailchimp