JavaScript何时何地运行,PHP如何?我能把两者结合起来吗? [英] When and where does JavaScript run, how about PHP? Can I combine the two?

查看:127
本文介绍了JavaScript何时何地运行,PHP如何?我能把两者结合起来吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么时候运行像JavaScript这样的客户端语言,什么时候运行像PHP这样的服务器端语言?我如何混合使用?



我想在点击我网站上的按钮时运行PHP函数,或者从PHP运行JavaScript函数;这可能吗?

解决方案

简短的回答是。你不能从JavaScript [AJAX除外] 运行PHP函数,也不能从PHP运行JavaScript函数。两个运行时间是分开的。



如何?



要了解JavaScript和PHP如何合作,首先应该了解支持网络的HTTP协议的基础知识。





上图演示了HTTP协议的基础知识。用户(您)要求客户端(您的浏览器)获取页面。然后,浏览器将向服务器(在此示例中为Google)询问该页面。服务器将回复HTML页面,客户端解析该页面,并询问正确加载页面所需的图像,字体和任何其他资源。客户端然后将完成的页面呈现给用户。



那么JavaScript进来的位置是什么?



JavaScript是在客户端(即浏览器)中运行。因此,在服务器的响应到达后,JavaScript将运行 。让我们将它添加到我们的图表中。





如您所见,PHP执行不会持续存在。它被执行,然后在发送响应后结束。






如您所见,之间没有重叠PHP执行和JavaScript执行,因此实际上不可能使其中一个函数基于另一个的输入工作。



但是..但是..我听说过AJAX!



AJAX只是从JavaScript 引起另一个HTTP请求。您可以将其称为一种使用JavaScript中的PHP函数的方法,但实际上并非如此。





正如您所看到的,使用AJAX,JavaScript会向服务器发送请求,该服务器将调用PHP,PHP将再次运行,如在正常的请求中(PHP不一定知道这甚至是一个AJAX请求!)并且服务器将响应返回给JavaScript,后者使用它来执行操作。



在这种情况下,PHP运行的时间和JavaScript运行的时间之间存在重叠,因为JavaScript调用了一个请求。



另见:




When does a client-side language like JavaScript run and when does a server-side language like PHP run? How can I mix both?

I want to run a PHP function when a button on my site is clicked, or run a JavaScript function from PHP; is that possible?

解决方案

The short answer is No. You cannot run PHP functions from JavaScript[With the exception of AJAX], nor can you run JavaScript functions from PHP. The two run times are separate.

How?

To understand how JavaScript and PHP cooperate, you should first understand the basics of the HTTP protocol that powers the web.

The diagram above demonstrates the basics of the HTTP protocol. The user (you) asks the client (your browser) to fetch you a page. The browser will then ask the server (Google, in this example) for the page. The server will reply with an HTML page, the client parses that page, and asks for the images, fonts and any other resources needed to load the page correctly. The client then presents the completed page to the user.

So where does JavaScript come in?

JavaScript is run in the Client (i.e. the browser). So JavaScript runs after the response from the server has arrived. Let's add that to our diagram.

JavaScript scripts start running as soon as they are loaded, and will continue to run if they have event listeners waiting for events from the user (like clicking, typing, or moving around).

Where does PHP fit in?

PHP runs on the Server, the web server (Which is a program responsible for serving web content) will run PHP according to its configuration. PHP will process the input from the web server, and return output. That output is served back to the client.

Updated diagram:

As you can see, the PHP execution does not persist. It is executed, and then ends once the response is sent.


As you can see, there is no overlap between the PHP execution and the JavaScript execution, so it isn't actually possible to make a function on one of them to work based on input from the other.

But.. but.. I've heard of AJAX!

AJAX is merely causing another HTTP request from JavaScript. You can call it a way to use PHP functions from JavaScript, but it's actually not quite that.

As you can see, with AJAX, JavaScript will send a request to the server, which will invoke PHP, PHP will run again, like in a normal request (PHP doesn't necessarily knows that this is even an AJAX request!) and the server returns the response back to JavaScript, which uses it to do stuff.

In this case, there is an overlap between the time PHP runs and the time JavaScript runs, since JavaScript invoked a request.

Also see:

这篇关于JavaScript何时何地运行,PHP如何?我能把两者结合起来吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆