是否可以在js脚本中包含一个php文件并访问该php文件的功能?

时间:2022-12-06 10:31:25

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

我有js文件,我需要调用php文件中定义的函数来计算值,然后继续下一步。这可能吗?我无法从我的脚本访问php文件的功能。还有一些方法可以在html页面中包含php文件,而无需更改扩展名.html

4 个解决方案

#1


5  

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

不,你做不到。 PHP和Javascript不在同一环境中执行,也不能在同一时间执行。

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.

在页面提供之前,PHP在您的Web服务器(服务器端)上执行。在页面提供后,Javascript在CLIENT的浏览器上执行。

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.

要从javascript调用PHP函数,您需要进行AJAX调用。这会将一个值从客户端发送到可以处理它的服务器端,然后将响应发送回客户端。

#2


3  

Try JQuery Ajax

试试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

用您的php文件替换yourphpfile.php并执行您的php文件

#3


1  

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

如果要访问服务器端资源(文件系统编写,数据库),则需要使用AJAX来回切换。

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

如果您只是熟悉PHP,那么您可以查看http://phpjs.org/,它在Javascript中重新实现了许多PHP函数。

#4


0  

PHP files are files that are executed by the server.

PHP文件是由服务器执行的文件。

  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. “我有js文件,我需要调用php文件中定义的函数来计算值。”浏览器无法运行php函数,需要服务器才能执行此操作。

  3. "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.
  4. “还有一些方法可以在html页面中包含php文件,而无需更改扩展名.html”如果你这样做,你的php代码将无法运行,因为服务器使用扩展来知道文件是否需要执行。

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调用PHP函数,您将需要AJAX。该值从客户端发送到处理它的服务器,然后将响应发送回客户端。

#1


5  

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

不,你做不到。 PHP和Javascript不在同一环境中执行,也不能在同一时间执行。

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.

在页面提供之前,PHP在您的Web服务器(服务器端)上执行。在页面提供后,Javascript在CLIENT的浏览器上执行。

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.

要从javascript调用PHP函数,您需要进行AJAX调用。这会将一个值从客户端发送到可以处理它的服务器端,然后将响应发送回客户端。

#2


3  

Try JQuery Ajax

试试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

用您的php文件替换yourphpfile.php并执行您的php文件

#3


1  

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

如果要访问服务器端资源(文件系统编写,数据库),则需要使用AJAX来回切换。

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

如果您只是熟悉PHP,那么您可以查看http://phpjs.org/,它在Javascript中重新实现了许多PHP函数。

#4


0  

PHP files are files that are executed by the server.

PHP文件是由服务器执行的文件。

  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. “我有js文件,我需要调用php文件中定义的函数来计算值。”浏览器无法运行php函数,需要服务器才能执行此操作。

  3. "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.
  4. “还有一些方法可以在html页面中包含php文件,而无需更改扩展名.html”如果你这样做,你的php代码将无法运行,因为服务器使用扩展来知道文件是否需要执行。

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调用PHP函数,您将需要AJAX。该值从客户端发送到处理它的服务器,然后将响应发送回客户端。