473,563 Members | 2,668 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.ph p?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 2192
d
"Simon" <sp********@exa mple.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.ph p?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********@exa mple.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>Ex ample</title></head>
<body onload="documen t.getElementByI d('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="javas cript"
type="text/javascript">par ent.document.ge tElementById("o utput").innerHT ML="'.$msg.'"; </script>';
flush();
}

set_time_limit( 0);

function long_ass_proces s($e) {
sleep(1);
}

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

foreach ($massive_array as $i=>$element) {
out('Processing #'.$i);
long_ass_proces s($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********@exa mple.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.co m> wrote:
^^^^^^^^^^^
"Simon" <sp********@exa mple.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*********@we b.de> wrote in message
news:57******** ********@Pointe dEars.de...
d <d@example.co m> wrote:
^^^^^^^^^^^
"Simon" <sp********@exa mple.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_sc ript.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($qu ery);
while ($record = mysql_fetch_arr ay($result)) {
// Process your records
$last_processed = $record['id'];
}
if (mysql_num_rows ($result) == 100) {
header('Locatio n: ' . $_SERVER['PHP_SELF'] .
"?last_processe d=$last_process ed");
echo "$last_processe d records processed so far... Please wait...";
die();
}
echo "Finished processing; $last_processed records were processed.";

Cheers,
NC

Mar 4 '06 #10

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

Similar topics

3
8486
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: this.xmlDoc= new ActiveXObject("Microsoft.DomDocument"); this.xmlDoc.load(url);
3
3262
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 solve the problem. You can find the article at http://www.codeproject.com/java/javaappletwaitmsg.asp I have adapted the code to my situation but...
5
5190
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 to wait for the external resource to load to show up the page, thus giving a perceiving faster load time for the user.
17
2754
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 language="javascript" src="../scripts/base.js" type="text/javascript" /> } </script> Obviously this would cause n error but this would give you an idea of what...
1
3790
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 = document.createElement("SCRIPT"); newScr.src = "newScr.js"; newScr.type="text/javascript";
2
2831
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 encountering a very mysterious bug that I can't seem to get rid of. When loading the page in Internet Explorer, everything works, and in Firefox it works...
6
2753
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: 1. document.write("<script src= language='JavaScript'></script>); 2. sc = document.createElement("<script>"); sc.setAttribute("src", ); and...
6
2205
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 fine ;) ) - If I have several pages all using 'somejs.js' file this file is shared on disk and is downloaded only once... but how about in memory,...
1
2839
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 associated errors that I am getting from the process. =( Now the original XML file is generated from a Java based Lotus Notes Agent. I have tested...
20
4011
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 and populates into the webpage; however, executing javascript has a few problems: 1. Opens on a blank page, but not a new page. It appears to...
0
7583
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7885
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8106
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7638
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7948
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6250
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3642
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
923
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.