469,935 Members | 1,983 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,935 developers. It's quick & easy.

Embeding javascript in php

196 100+
I know that you can echo html in a .php page, but can you echo html with a javascript aswell?

For example

Just html
Expand|Select|Wrap|Line Numbers
  1. echo '<p>' . "hello world" . '</p>';
Then with javascript

Expand|Select|Wrap|Line Numbers
  1. echo '<script type="text/javascript">
  2. document.write("Hello World!");
  3. </script>';
Any help with this or a similar method would be greatly appreciated thanks,


Don't worry i answered my own question lol thanks anyway
Jul 12 '08 #1
1 1231
Right, you've answered your own question: Yes, you can!

BUT... You're really mixing up everything here. I recommend you try Smarty ( http://www.smarty.net/ ) and keep your pages separated in three files:
- page.php - contains the _php code_, initializes the smarty object and sets variables, includes page.tpl
- page.tpl - contains the html code mixed with _smarty templates_ and includes the javascripts required
- page.js - contains the _javascript code_

If you don't split things up, you'll mash everything up and it will be impossible for anyone to understand what you are doing. Using PHP to echo JavaScript code so that it would come out in the HTML page is just awful :o)
Jul 14 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Sascha Kerschhofer | last post: by
2 posts views Thread by BigOne | last post: by
2 posts views Thread by Raj Dhrolia | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.