通过Javascript加载PHP文件/代码。 [英] Loading a PHP file/code via Javascript.

查看:73
本文介绍了通过Javascript加载PHP文件/代码。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




对于交叉发布很抱歉,但我认为它适用于这两种语言。


我们都知道,JavaScript客户端和php是服务器端,(php

代码''允许''在JavaScript服务器上做的事情是不能的。)

php的问题是一段时间后它会超时,(并且用户也是

对于长时间发生的事情一无所知)。

我需要运行一个脚本在服务器上可能需要很长时间。


所以我想的是混合使用JavaScript和PHP

类似的东西,


< script>

var endvalue = 1000; / *服务器可以计算的一些数字

快速* /

var i = 0

while(i< = endvalue)

{

/ **

调用一个可以做一些工作的php文件

somefunction.php?someNumber = i

* /

}

< / script>


这样服务器就能完成工作,而客户端继续前进。

理想情况下,我也会从php脚本中获得返回值/字符串。


我怎么能实现类似的东西?


非常感谢提前。


Simon

解决方案

< BLOCKQUOTE>"西蒙" < SP ******** @ example.com>在消息中写道

news:46 ************ @ individual.net ...


抱歉交叉发布,但我认为它适用于这两种语言。

众所周知,JavaScript是客户端,php是服务器端,(php
代码是''允许''在JavaScript的服务器上做的事情不能这样做。)
php的问题是它在一段时间后超时,(并且用户也不知道发生了什么事情很长一段时间。
我需要在服务器上运行一个可能需要很长时间的脚本。

所以我想的是混合JavaScript和PHP
类似的东西,

< script>
var endvalue = 1000; / *服务器可以快速计算的一些数字* /
var i = 0
while(i< = endvalue)
{
/ ** 调用一个可以做一些工作的php文件
somefunction.php?someNumber = i
* /
}
< / script>

那个服务器完成工作的方式,而客户端继续工作。
理想情况下,我也会从php脚本中获得返回值/字符串。

我怎样才能实现这样的目标呢? br />
非常感谢提前。

Simon




我自己做过类似的事情:)


我发现最好的方法是让你的冗长的PHP脚本运行在

a隐藏的iframe中,偶尔会吐出大量的javascript(完整的

< script>标记),并在写入每个标记后刷新输出。

javascript可以更新文件显示,或进度条或其他任何内容,即

持有iframe的文档。它的使用非常简单,非常有效。$ / b

dave


另一个选项是使用CURL。写另一个php页面,它需要一个

的参数并将参数传递给这个页面。 CURL存在

一大堆语言。


不是我自己已经探索过这个,但我想AJAX是第三种选择。 />
查看xmlHttpRequest及其对等功能。


祝你好运!

- Peder -



我自己做了类似的事情:)

我找到了最好的办法这是让你的冗长的PHP脚本在一个隐藏的iframe中运行
,偶尔吐出一堆javascript(在
完整的< script>标签中),并在每个标签
写的。 javascript可以在持有iframe的文档上更新文本显示或进度条
或其他任何内容。使用起来非常简单,非常有效。

dave




嗯,你有一个小例子吗?我可以试试吗?


''因为我不确定我跟着,如果php在30秒后超时,(

默认),那么真的没有什么可以做的。

我需要将工作分成不到30秒的块,并以某种方式得到

JavaScript来调用每个''chunck''一次一个。


所以我需要JavaScript来打开一个PHP脚本,等待输出和

处理输出。


是不是可以在服务器上调用另一个文件JavaScript并回显

输出?


我希望我的解释清楚。


Simon


Hi,

Sorry for the cross posting, but I think it applies to both languages.

As we all know, JavaScript is client side and php is server side, (the php
code is ''allowed'' to do stuff on the server that JavaScript cannot).
The problem with php is that it timeout after a while, (and the user also
has no clue as to what is going on for a long time).
I need to run a script on the server that could take a very long time.

So what I was thinking is mixing both JavaScript and PHP
Something like,

<script>
var endvalue = 1000; /* some number that the server can calculate
quickly */
var i = 0
while (i<=endvalue)
{
/**
call a php file that will do some work
somefunction.php?someNumber=i
*/
}
</script>

That way the server does the work, while the client keeps it going.
Ideally I would also get a return value/string from the php script.

How could I achieve something like that?

Many thanks in advance.

Simon

解决方案

"Simon" <sp********@example.com> wrote in message
news:46************@individual.net...

Hi,

Sorry for the cross posting, but I think it applies to both languages.

As we all know, JavaScript is client side and php is server side, (the php
code is ''allowed'' to do stuff on the server that JavaScript cannot).
The problem with php is that it timeout after a while, (and the user also
has no clue as to what is going on for a long time).
I need to run a script on the server that could take a very long time.

So what I was thinking is mixing both JavaScript and PHP
Something like,

<script>
var endvalue = 1000; /* some number that the server can calculate
quickly */
var i = 0
while (i<=endvalue)
{
/**
call a php file that will do some work
somefunction.php?someNumber=i
*/
}
</script>

That way the server does the work, while the client keeps it going.
Ideally I would also get a return value/string from the php script.

How could I achieve something like that?

Many thanks in advance.

Simon



I''ve done something similar to this myself :)

I found the best way to do it is to have your lengthy PHP script running in
a hidden iframe, occasionally spitting out chunks of javascript (in complete
<script> tags), and flushing the output after each tag is written. The
javascript can update a textual display, or a progress bar or whatever, on
the document holding the iframe. It''s really simple to use, and very
effective.

dave


Another option is to use CURL. Write another php-page that takes a
couple of parameters and pass the parameters to this page. CURL exists
for a whole bunch of languages.

Not that I''ve explored this myself, but I guess AJAX is a third option.
Check out xmlHttpRequest and it''s peer functions.

Good luck!
- Peder -



I''ve done something similar to this myself :)

I found the best way to do it is to have your lengthy PHP script running
in a hidden iframe, occasionally spitting out chunks of javascript (in
complete <script> tags), and flushing the output after each tag is
written. The javascript can update a textual display, or a progress bar
or whatever, on the document holding the iframe. It''s really simple to
use, and very effective.

dave



Hum, would you have a small example I could try?

''cause I am not sure I follow, if the php times-out after 30 seconds, (the
default), then there is nothing it can do really.
I need to divide the work in chunks of less that 30 seconds and somehow get
the JavaScript to call each ''chunck'' one at a time.

So I would need JavaScript to open a php script, wait for the output and
handle the output.

Is it not possible to call another file on the server JavaScript and echo
the output?

I hope my explanation is clear.

Simon


这篇关于通过Javascript加载PHP文件/代码。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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