• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Calling Javascript from PHP Code

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7135
1360
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I always use echo "<script> function(); </script>";
 
Devaka Cooray
Sheriff
Posts: 7135
1360
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic