• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling Javascript from PHP Code

 
Anand Sonus
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm in need of piece of code which will call javascript function from PHP code. Here PHP and javascript are different files. Please anyone help me on this.

Thanks
 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4252
227
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch
Do you understand that PHP runs in the server and Javascript runs in the client (browser) so "calling JavaScript function from PHP" is meaningless? If you TellTheDetails with what exactly you need to get done, we can probably come up with a better useful suggestion.
 
Anna Harris
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I always use echo "<script> function(); </script>";
 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4252
227
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anna Harris wrote:I always use echo "<script> function(); </script>";

It doesn't work in the way you think it does. You don't even need to have an "echo"- just placing <script> function(); </script> outside <?php ?> tags does the same as what with "echo" does. Particularly, it 'prints' that fragment as a text in the HTML content, and your browser interprets the generated HTML and there is a Javascript snippet calling your function - that's now something like calling-JS-from-PHP.

If calling to a Javascript method in a separate file is all what you need, simply 'import' that Javascript file using <script src> in your <head> block, and then you can call its functions anywhere in the body as you always do.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic