• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

Calling Javascript from PHP Code

 
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
 
Sheriff
Posts: 6601
1253
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • 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.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I always use echo "<script> function(); </script>";
 
Devaka Cooray
Sheriff
Posts: 6601
1253
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • 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.
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic