473,407 Members | 2,546 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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
Mar 3 '06 #1
10 2184
d
"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
Mar 3 '06 #2
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 -

Mar 3 '06 #3

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
Mar 3 '06 #4
d
"Simon" <sp********@example.com> wrote in message
news:46************@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
Mar 3 '06 #5
>
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
Mar 3 '06 #6
d
"Simon" <sp********@example.com> wrote in message
news:46************@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?
Mar 3 '06 #7
d <d@example.com> wrote:
^^^^^^^^^^^
"Simon" <sp********@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
Mar 4 '06 #8
d
"Thomas 'PointedEars' Lahn" <Po*********@web.de> wrote in message
news:57****************@PointedEars.de...
d <d@example.com> wrote:
^^^^^^^^^^^
"Simon" <sp********@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

Mar 4 '06 #9
NC
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

Mar 4 '06 #10

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.
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...ming-seed.html

Chris S.

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

Mar 5 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Roy Wang | last post by:
hi, My problem is how to determining when the XML file has loaded using javascript. I loaded an xml file using javascript in a web page. The code below is loading the xml file for IE:...
3
by: Jacques Chaurette | last post by:
Hello all , thanks in advance for any help. While looking for a solution to how to replace the grey box while an applet loads, a search of Google got me an article by Glenn s. Peffers that claims to...
5
by: joaopedrogoncalves | last post by:
Hi, I want to load an external javascript file, get its results and stick them inside a <div> block. I also want to do this in several places on a web page. This way the browser doesn't have...
17
by: CES | last post by:
All, I was wondering if their is a way of loading an external script fill from within a script?? <script language="javascript" type="text/javascript"> function test(var){ <script...
1
by: ozzy.osborn | last post by:
Hello All, I have been struggling with a cross browser solution to loading external javascript files on the fly. I have been successful using the following code in IE6: var newScr =...
2
by: nick.taylor | last post by:
Hi, I've been trying for weeks to figure out this problem. I'm developing a simple Javascript app that loads an XML file from a server, parses the contents, and displays them. But I am...
6
by: Venkatesh | last post by:
Hello All, I have couple of doubts regarding the concept of on-demand javascript loading using javascript code. I could see on the net different techniques for achieving this - techniques like:...
6
by: ernesto.tejeda | last post by:
Hello, I have a couple of questions regarding the loading of .js files for the browser and would like anyone to point me wher to find the answer (or if you know the answer and tell me will do just...
1
by: FreeXenon | last post by:
OK, I am having a lot of problems loading an XML file via a Server-Side JavaScript based ASP page. Below is the code that I am using as well as the different methods of loading the XML file and the...
20
RMWChaos
by: RMWChaos | last post by:
Currently testing in: WinVista / IE7 I have been working on getting xmlhttprequest going for weeks now. I have finally gotten a semi-working script going. Pulling up text or xml files works great...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.