2 Replies - 3638 Views - Last Post: 07 May 2010 - 03:07 PM

#1 lelouch23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 26-March 10

call php function from javascript

Posted 06 May 2010 - 07:28 AM

hello there anyone know how to call php function from javascript.
eg. i want to call this function inside javascript.
<?php
function display(){
echo 'hello';
}
?>
Is This A Good Question/Topic? 0
  • +

Replies To: call php function from javascript

#2 Fratyr  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 139
  • Joined: 10-April 08

Re: call php function from javascript

Posted 06 May 2010 - 07:53 AM

You can't.

Learn how web pages works.

First, your browser sends request to the server, then the server starts the PHP parser, parses all your code and outputs HTML, then it returns to your page.

When browser received the HTML, it started to render it on a visible area, if he sees <script> tag, it initializes Javascript codes.

So, you can't run server side functions with client side function, BUT!

There's one cool thing in Web, called AJAX. You can, with javascript initialize request to the server, run a function in another file and retrieve function's output to browser.

Take a look at AJAX Tutorials and HOWTOs.
Was This Post Helpful? 0
  • +
  • -

#3 jrm402  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 51
  • View blog
  • Posts: 333
  • Joined: 18-March 10

Re: call php function from javascript

Posted 07 May 2010 - 03:07 PM

Here's a little bit to help you understand PHP/Javascript and the difference between server and client scripting languages. Go to your PHP file in your browser. View the source. This is the HTML that the server sent to the browser after processing the PHP code. When the browser receives the HTML, which is all it can understand (browsers don't understand PHP code), it then starts to process that. When/if it finds Javascript it executes it. Think of HTML as kind of a client side scripting language too (which it really isn't). It is processed and executed by the browser on the client machine.

Help any? :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1