Loading a PHP file/code via Javascript.

This is a discussion on Loading a PHP file/code via Javascript. within the PHP Language forums, part of the PHP Programming Forums category; Hi, Sorry for the cross posting, but I think it applies to both languages. As we all know, JavaScript is ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-03-2006
Simon
 
Posts: n/a
Default Loading a PHP file/code via Javascript.

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


Reply With Quote
  #2 (permalink)  
Old 03-03-2006
d
 
Posts: n/a
Default Re: Loading a PHP file/code via Javascript.

"Simon" <spambucket@example.com> wrote in message
news:46re0iFcj4evU1@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


Reply With Quote
  #3 (permalink)  
Old 03-03-2006
pyda001@ec.auckland.ac.nz
 
Posts: n/a
Default Re: Loading a PHP file/code via Javascript.

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 -

Reply With Quote
  #4 (permalink)  
Old 03-03-2006
Simon
 
Posts: n/a
Default Re: Loading a PHP file/code via Javascript.


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


Reply With Quote
  #5 (permalink)  
Old 03-03-2006
d
 
Posts: n/a
Default Re: Loading a PHP file/code via Javascript.

"Simon" <spambucket@example.com> wrote in message
news:46rh13Fc9380U1@individual.net...
>
>>
>> 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?


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


Reply With Quote
  #6 (permalink)  
Old 03-03-2006
Simon
 
Posts: n/a
Default Re: Loading a PHP file/code via Javascript.

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


Reply With Quote
  #7 (permalink)  
Old 03-03-2006
d
 
Posts: n/a
Default Re: Loading a PHP file/code via Javascript.

"Simon" <spambucket@example.com> wrote in message
news:46rl87Fcll4pU1@individual.net...
> >
>> 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.


No problem ;)

> Simon


What restrictions do you have under your specific safe mode?


Reply With Quote
  #8 (permalink)  
Old 03-04-2006
Thomas 'PointedEars' Lahn
 
Posts: n/a
Default Re: Loading a PHP file/code via Javascript.

d <d@example.com> wrote:
^^^^^^^^^^^
> "Simon" <spambucket@example.com> 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
Reply With Quote
  #9 (permalink)  
Old 03-04-2006
d
 
Posts: n/a
Default Re: Loading a PHP file/code via Javascript.

"Thomas 'PointedEars' Lahn" <PointedEars@web.de> wrote in message
news:5774309.bp8jj66Spa@PointedEars.de...
>d <d@example.com> wrote:
> ^^^^^^^^^^^
>> "Simon" <spambucket@example.com> 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?


And if you read RFC 2606 section 3, you will see that example.com is not
available for use, so no-one's being abused.

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



Reply With Quote
  #10 (permalink)  
Old 03-04-2006
NC
 
Posts: n/a
Default Re: Loading a PHP file/code via Javascript.

Simon wrote:
>
> 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

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 12:02 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0