Loading a PHP file/code via Javascript.

S

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
 
D

d

Simon said:
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
 
P

pyda001

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

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

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
 
D

d

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

----------------------------------------------------------------------
index.html:

<html>
<head><title>Example</title></head>
<body onload="document.getElementById('ifr').src='/script.php';">
<iframe id="ifr" style="display: none;" src=""></iframe>
<div id="output"></div>
</body>
</html>
----------------------------------------------------------------------

----------------------------------------------------------------------
script.php:

<html><head></head><body><?

function out($msg) {
echo '<script language="javascript"
type="text/javascript">parent.document.getElementById("output").innerHTML="'.$msg.'";</script>';
flush();
}

set_time_limit(0);

function long_ass_process($e) {
sleep(1);
}

$massive_array=array_pad(array(), 200, array());

foreach ($massive_array as $i=>$element) {
out('Processing #'.$i);
long_ass_process($element);
}

out('Done.');

?></body>
</html>
----------------------------------------------------------------------

(I've not tested it, btw)
'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.

use set_time_limit(0) to disable the time-out.
So I would need JavaScript to open a php script, wait for the output and
handle the output.

Not if you disable the time-out :)
Is it not possible to call another file on the server JavaScript and echo
the output?

No need :)
I hope my explanation is clear.

Simon

I hope this helps!

dave
 
S

Simon

set_time_limit(0);

I am running php in safe mode and I cannot change that.
use set_time_limit(0) to disable the time-out.

Ah, but I cannot do that unfortunately.

Thanks for that, but I cannot change the timeout stuff.

Simon
 
D

d

Simon said:
I am running php in safe mode and I cannot change that.


Ah, but I cannot do that unfortunately.

Thanks for that, but I cannot change the timeout stuff.

No problem ;)

What restrictions do you have under your specific safe mode?
 
T

Thomas 'PointedEars' Lahn

d said:
"Simon" <[email protected]> wrote [...]
^^^^^^^^^^^
I am curious: Do your service providers already know about your
ongoing domain abuse? Does IANA, the owner of the domain?

And will you ever stop crossposting off-topic without Followup-To?


X-Post & F'Up2 news.admin.net-abuse.misc

PointedEars
 
N

NC

Simon said:
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.

Does the user need to see anything based on the results of this long
process?
How could I achieve something like that?

There are two options I can think of; neither involves JavaScript. I
am sure there are many other options, too.

1. Use command-line scripting

If there is no need for the user to see the output, you can start your
processing script in the background using the command-line interpreter.
On Unix, this will look something like this:

exec('php my_very_long_script.php &');

2. Use "piecemeal" processing

Say, you need to process an unknown number of records in a database.
You estimated that processing 100 records at a time is done quickly
enough not to trigger the execution time limit. So you can repeatedly
run the same script to process a new group of records each time;
something like this:

if (isset($_GET['last_processed'])) {
$last = (int) $_GET['last_processed'];
$query = "SELECT * FROM mytable WHERE id>$last ORDER BY id LIMIT
100";
} else {
$query = "SELECT * FROM mytable ORDER BY id LIMIT 100";
}
$result = mysql_query($query);
while ($record = mysql_fetch_array($result)) {
// Process your records
$last_processed = $record['id'];
}
if (mysql_num_rows($result) == 100) {
header('Location: ' . $_SERVER['PHP_SELF'] .
"?last_processed=$last_processed");
echo "$last_processed records processed so far... Please wait...";
die();
}
echo "Finished processing; $last_processed records were processed.";

Cheers,
NC
 
G

google

Simon said:
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.
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?

Hello Simon,

I've put together a free, very simple AJAX tool that you can use to
accomplish the client / server information transfer:

Super AJAX Programming Seed
http://www.impliedbydesign.com/super-ajax-programming-seed.html

Chris S.

Free Web Design Tools
http://www.impliedbydesign.com/free-software-scripts.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top