Ajax如何将Javascript字符串传递给php

我一直在学习Ajax,现在我想知道如何将Javascript中的字符串传递给php 。</ p>

之前有人说,将Javascript传递给PHP的问题是首先运行PHP代码,然后运行Javascript。 所以当Javascript生成一个字符串时已经太晚了。</ p>

这是否意味着Ajax允许在Javascript之后运行PHP代码?</ p>
</ div>

展开原文

原文

I've been learning Ajax and now I'm wondering how it allows a string from Javascript to be passed to php.

It was said before that the problem with passing Javascript to PHP is that the PHP code gets run first, and then the Javascript gets run. So when Javascript generates a string it's already too late.

Does this mean that Ajax allows PHP code to be run after Javascript?

4个回答



我认为这就是他们所得到的:</ p>

在Ajax之前 - 具体来说, 在 XMLHttpRequest 出现之前 - 单个网页作为单页面加载。 如果它是一个包含Javascript的PHP生成的页面,浏览器将请求页面,PHP将生成页面(包括Javascript代码,包括页面上的脚本片段等),将页面发送到浏览器,以及 浏览器会显示它。 所以,PHP发生在前面。 在下一页加载之前 - 当整个</ em>页面从头开始刷新时 - PHP没有再次参与。</ p>

XMLHttpRequest ,它帮助将“X”放在“AJAX”中,就像当时那样,你有另一种选择。 加载页面后,您的Javascript可以在页面的“幕后”发出请求,从服务器请求更多信息,而无需重新加载页面。 实际上,加载的页面可能会导致更多PHP在服务器上运行,并显示结果。</ p>

因此,如果您正在考虑从基于PHP的网站加载单页 ,这是(某种)Ajax的意思; 如果没有它,您将获得一个PHP页面构建,然后您的Javascript必须单独运行该结果。 使用Ajax,您可以向服务器发出进一步的请求,并将结果输出到现有页面而不会导致整页加载。</ p>
</ div>

展开原文

原文

I think this is what they're getting at:

Before Ajax -- specifically, before XMLHttpRequest came along -- a single web page was served as a single page load. If it was a PHP-generated page incorporating Javascript, the browser would request the page, PHP would generate the page (including Javascript code, includes, fragments of script on the page, etc.), would send the page to the browser, and the browser would display it. So, the PHP happened up-front. Until the next page load -- when the entire page was refreshed from scratch -- PHP wasn't involved again.

After the advent of XMLHttpRequest, which helped put the "X" in "AJAX", as it was back then, you had another option. Once the page was loaded, your Javascript could make requests "behind the scenes" of the page, to request more information from the server, without reloading the page. In effect, the loaded page could cause more PHP to be run on the server, and display the results.

So, if you're considering a single page load from a PHP-based website, that is (sort of) what Ajax means; without it, you get a single PHP page-build that's delivered and then your Javascript has to run on that result alone. With Ajax, you can make further requests to your server and throw the results out onto the existing page without a full page load.



坐在服务器上的php解释器基本上将任何php脚本解释为(通常)HTML页面。</ p>

这就是为什么你永远不能将javascript变量“传递”到php中作为解释器,你的javascript只是另一个字符串,没有任何特殊含义。 你的javascript是由你的浏览器运行的,甚至不知道它是由PHP生成的。</ p>

我认为这是“太晚了”的含义。</ p>
</ DIV>

展开原文

原文

The php interpreter sitting on the server is basically interprets whatever php script into (usually) HTML page.

That's why you can never "pass" javascript variable into php as to the interpreter, your javascript is just yet another string, without any special meaning. Your javascript is run by your browser and doesn't even aware that it was being produced by PHP.

I believe that's what it means by "too late".



你应该知道,Javascript总是(Node.js除外)客户端。

PHP是服务器端语言。< / p>

您无法将Javascript变量传递给PHP - 至少不能传递给Pageload。</ p>

</ em> </ em>所做的是 在页面被装入以向PHP发送内容之后执行AJAX请求。
使用该调用的响应,您可以替换当前请求的页面上的其他一些内容。</ p>
</ div>

展开原文

原文

You should know, that Javascript is always (except Node.js) Client-Side. PHP is a Server-Side language.

You can't pass Javascript variables to PHP - at least not at the Pageload.

What you can do is doing a AJAX-Request after the Page is laoded to send something to PHP. With the Response of that call you can replace some other things on the current requested Page.



否</ strong>这并不意味着“Ajax允许在Javascript之后运行PHP代码”。 AJAX是服务器的新请求</ em>,它允许您使用服务器的响应</ em>进行操作。 您可以通过处理正在进行AJAX请求的URL并将其粘贴到浏览器中来观察此情况。

基本上当您使用浏览器打开网站时,您发送请求并且任何AJAX调用也是请求 ,但在后台完成,所以你不能直接看到它。 您可以使用例如firebug或其他开发人员的工具来查看幕后发生的情况。 它与脚本执行命令无关。 </ p>
</ div>

展开原文

原文

No it does not mean that "Ajax allows PHP code to be run after Javascript". AJAX is a new request to the server and it allows you to manipulate with the server's response. You can observe this by coping the url to which AJAX request is being made and pasting it into the browser.
So basically when you open a web site with the browser, you send a request and any AJAX call is also a request, but done in a background, so you cannot see it directly. You can use for example firebug or other developer's tool to see what happens behind the scenes. It has nothing to do with the scripts executions orders.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐