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
- 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
4 Answers
Reset to default 6No, 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.
- "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.
- "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.
本文标签:
版权声明:本文标题:javascript - Is it possible to include a php file in js script and access the function of that php file? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741268282a2368875.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论