admin管理员组

文章数量:1287577

I have js file where i need to call the function defined in the php file to calculate the value and then move on to next step. Is this possible to do? I am not able to access the function of the php file from my script. Also is there some way around to include php file in a html page without changing the extention .html

I have js file where i need to call the function defined in the php file to calculate the value and then move on to next step. Is this possible to do? I am not able to access the function of the php file from my script. Also is there some way around to include php file in a html page without changing the extention .html

Share Improve this question asked Jun 27, 2012 at 6:43 user850234user850234 3,46315 gold badges52 silver badges84 bronze badges 1
  • 1 I don't think so. You can though make an Ajax call to access the PHP file – skos Commented Jun 27, 2012 at 6:45
Add a ment  | 

4 Answers 4

Reset to default 6

No, you cannot do that. PHP and Javascript do not execute in the same environment, nor at the same time.

PHP executes on YOUR web server (server-side) before the page is served up. Javascript is executed on the CLIENT's browser after the page has been served up.

To call a PHP function from javascript, you will need to make an AJAX call. This sends a value from the client side to the server side where it can be processed, and the response is then sent back to the client.

Try JQuery Ajax

It´s easy to use.

$.ajax({
  type: "POST",
  url: "yourphpfile.php",
  data: { values: "100"}
}).done(function( msg ) {
  console.log("php file called"+msg);
});

replace yourphpfile.php with your php file and your php file gets executed

If you want to access server-side resources (filesystem writing, database), you'd need to use AJAX to switch back and forth.

If you're just more familiair with PHP, you could take a look at http://phpjs/, which reimplements many PHP functions in Javascript.

PHP files are files that are executed by the server.

  1. "I have js file where i need to call the function defined in the php file to calculate the value." Browsers cannot run php functions, you need server to do that.
  2. "Also is there some way around to include php file in a html page without changing the extention .html" If you do that your php code will not run, because server use the extensions to know if a file needs execution or not.

To call a PHP function from javascript, you will need AJAX. The value is send from the client to the server where it is processed, and the response is then sent back to the client.

本文标签: