Solved

Run php in background including a "loading" effect

Posted on 2004-09-13
29
2,708 Views
Last Modified: 2013-11-18
Dear All,

I have a php popup page which connect db to greb some information, but the process takes time..so I would like to have a animated gif with word "processing ... ..." to tell the user the status.

once the db connection is finished, I will ask this popup page to call a javascript to refresh its parent page and then close itself automatically.

but I found that I the gif usually is not loaded and the browser is closed once the query is finished (the browser status is "pending", seems "hold" as not all component are loaded). so I can't have the "loading" effect that I expected.

so what should I do in this case??

0
Comment
Question by:ping1234
  • 7
  • 5
  • 4
  • +4
29 Comments
 
LVL 1

Accepted Solution

by:
maunded earned 20 total points
Comment Utility
I have a similar function on one of my sites, I build the page and load the gif first, then run the query, when the query returns its result I use an if statement to do: print ("<META HTTP-EQUIV='refresh' content='1;URL=mypage.php?action=1'>");


0
 

Author Comment

by:ping1234
Comment Utility
could you please explain a little on
print ("<META HTTP-EQUIV='refresh' content='1;URL=mypage.php?action=1'>");

??

I don't know whether my query time is not long enough..so the animated gif does not show in my case..please advise..
0
 
LVL 1

Expert Comment

by:maunded
Comment Utility
Sure, once the query has run it will (should) return a result, for me thats a 1 if the query is successful or a 0 if its not.
I run my query as such:
$result = mssql_query($query);
IF ($result == "1") {      
print ("<META HTTP-EQUIV='refresh' content='$rand;URL=mypage.php?action=1'>");
}
ELSE {
print ("There has been an error sending your data, please contact System Administrator");
exit(); };

The line "print ("<META HTTP-EQUIV='refresh' content='$rand;URL=mypage.php?action=1'>");"
Sends the browser to another page which prints the results of the query. Otherwise it just outputs an error message
Can you possibly post your code?

0
 

Author Comment

by:ping1234
Comment Utility
the query that I have to execute...can said to be a "small program"..it includes several process...and some of them related to pop3  server connection..

and the page ordering should be

main.php -> onLoad -> then call popup -> popup.php execute the "small program that I mention and at the same time would like to have the loading effect (but not successful now..) -> once the "program" is finished -> call the parent page to refresh and close itself
0
 
LVL 1

Expert Comment

by:maunded
Comment Utility
OK, I think I understand you.
In the popup.php, build the html first, then call the small program, PHP will build the page, then run your query, then when that query is done, print the http-refresh.
0
 

Author Comment

by:ping1234
Comment Utility
thanks,

but..
this refresh statement refresh do not refresh the parent page in my case?

so I use

echo("<script language=\"Javascript\">");
echo("window.opener.location.reload();");
echo("self.close();");
echo("</script>");

actually I the animated gif code is already located first, and the php code follows..but I still can't have the loading effect..
0
 
LVL 36

Assisted Solution

by:Zyloch
Zyloch earned 20 total points
Comment Utility
Hi

It should reload, but it could depend on what browser you're using.

Try using
window.opener.location=window.opener.location.href;
instead.

Regards,
Zyloch
0
 

Assisted Solution

by:fetchmaster
fetchmaster earned 20 total points
Comment Utility
instead of meta refresh of the pop up, you can directly close the window just after refreshing the main.php page. its something like this:

popup.php
<?php
//your application code goes here........


//Now execution complete.....
?>
<script>
opener.document.location.reload(true);
window.close();
</script>

0
 

Author Comment

by:ping1234
Comment Utility
the reload part is ok..I just can't get the "loading" effect that I expected..
0
 
LVL 36

Expert Comment

by:Zyloch
Comment Utility
The main window is having the loading effect?
0
 

Author Comment

by:ping1234
Comment Utility
no, the popup one..please check pervious message..
0
 
LVL 36

Expert Comment

by:Zyloch
Comment Utility
Ah, I see. Try outputting the image without using PHP, something like:

<img src="processing.gif">
<?php
//Your processiong code
?>

if you aren't doing so already. It may not work because PHP may totally be processed first, but it's possible to try.
0
 
LVL 1

Expert Comment

by:maunded
Comment Utility
Does the page load at all before it runs the query?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:ping1234
Comment Utility
if I get the query/program commented, I can see my "processing ... ..." animated gif.

but if code are executed, I can just see a pending browser and then refresh the parent window and close itself.
0
 
LVL 36

Expert Comment

by:Zyloch
Comment Utility
Most likely because the PHP executes itself totally before outputting to the browser.

You can always try sending a chunked transfer:

header("Transfer-Encoding: chunked");
0
 
LVL 1

Expert Comment

by:maunded
Comment Utility
I output the html before doing anything in PHP
0
 

Author Comment

by:ping1234
Comment Utility
I have already output html before the execution of php...

finially..I've got a solution for my problem.
main.php -> onLoad -> then call popup -> popup.php execute the "small program that I mention and at the same time would like to have the loading effect  -> once the "program" is finished -> call the parent page to refresh and close itself

in the popup.php, I use frameset..top frame contains only a "processing....." gif and a hidden frame process the code, once the code is processed, it call the parent 's opener to refresh itself and then call parent to close.

Thanks for all of your help!
0
 
LVL 40

Assisted Solution

by:RQuadling
RQuadling earned 20 total points
Comment Utility
Try something like this ...

<html>
<head>
<title>KPI Production</title>
<link title="KPI Style Sheet" rel="stylesheet" href="kpi_css.php">
</head>
<body>
<div class="MainDIV">
      <a href="/"><img class="LogoIMG" src="./images/bandvulc_kpi_logo.gif" alt="Bandvulc Tyres KPI Production" /></a>
      <hr class="MainHR" />
</div>
<div class="PleaseWaitDIV" id="pleasewait">
      <h2 class="PleaseWaitH2">
            Please wait - generating results
      </h2>
      This may take some time.<br />
      If you want to abort the current operation, then click <a class="PleaseWaitA" href="/index.php">here</a> to return to the main menu.
      <br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
</div>
<?php
flush();
.....
// Include your PHP code here, but make sure it only generates a $sOutput.
?>
<script language="JavaScript"><!--
pleasewait.style.display = 'none';
document.body.style.cursor = 'auto';
//--></script>
$sOutput
END_HTML;
?>
</body>
</html>

I use this for all my internal apps.

The PleaseWait div shows whilst the page is being generated (normally involves getting data from a DB, sorting it out, building tables or a PDF, etc, and can take several seconds or even minutes if the request is a big analysis). The PHP code builds a text variable for the output (i.e. functions NEVER echo/print, only return) and the main app builds up the output from the functions). I COULD have used output buffering, maybe. But I didn't.

When the output is ready, the javascript "hides" the pleasewait div and then shows the real div.

Also, the cursor is changed from "busy" to "ready".

Only tested on Windows, IE6.

The please wait div COULD include any sort of output you want. An image which could be a flash thing which looks at a counter the main php code is generating maybe, to give you some sort of real time feedback.

Regards,

Richard.
0
 

Expert Comment

by:m1ckey_G
Comment Utility
Yo can try using output control.

<?php
ob_start ();
echo ('Your HTML here');
ob_flush ();

// Your query here
0
 
LVL 3

Assisted Solution

by:Sasho
Sasho earned 20 total points
Comment Utility
This is what I do on my pages:

<html>
<head>
      <script type="text/javascript" language="javascript">
      function swapContent(divID, srcDivID)
      {
              document.getElementById(divID).style.display='none';
              document.innerHTML = document.getElementById(srcDivID).innerHTML;
      }
      </script>
</head>
<body marginheight="0" marginwidth="0" leftmargin="0" topmargin="0">
      <DIV id='Wait_Text' STYLE="position:absolute; top:200;left:200; border:none thin gray;z-index:300">
            <table width="*" cellpadding="3" cellspacing="1" border="0" class="forumline">
                  <td class="Open"><h1>Please wait while the data is being processed...</h1.</td>
            </table>
      </DIV>
      <?
      flush();
      ?>
      <div id='Good' 'display:none;z-index:200'>
            <?
            //Your PHP goes here
            sleep(10);
            print("PHP is done!");
            //---------------------
            ?>
      </div>
      <SCRIPT LANGUAGE="JavaScript"><!--swapContent('Wait_Text','Good');//--></SCRIPT>
</body>
</html>
0
 
LVL 3

Expert Comment

by:Sasho
Comment Utility
Sorry my last comment had a cut and paste error which made it wrong. here is the code which I tested and works:

<html>
<head>
<script type="text/javascript" language="javascript">
function swapContent(divID, srcDivID)
{
  document.getElementById(divID).style.display='none';
  document.innerHTML = document.getElementById(srcDivID).innerHTML;
}
</script>
</head>
<body marginheight="0" marginwidth="0" leftmargin="0" topmargin="0">
<DIV id='Wait_Text' STYLE="position:absolute; top:200;left:200; border:none thin gray;z-index:300">
<table width="*" cellpadding="3" cellspacing="1" border="0" class="forumline">
<td class="Open"><h1>Please wait while the data is being processed...</h1.</td>
</table>
</DIV>
<?
flush();
?>
<div id='Good' 'display:none;z-index:200'>
<?
//Your PHP goes here
sleep(3);
print("Php is done");
?>
</div>
<SCRIPT LANGUAGE="JavaScript"><!--
      swapContent('Wait_Text','Good');
//--></SCRIPT>
</body>
</html>
0
 

Expert Comment

by:fetchmaster
Comment Utility
Sasho,


<div id='Good' 'display:none;z-index:200'>
should be
<div id='Good' style='display:none;z-index:200'>
i think?
0
 
LVL 40

Expert Comment

by:RQuadling
Comment Utility
Sasho and my code do pretty much the same thing. Show a "Please wait" sort of thing up front and then hide it when the results come through.

BTW, the JS code of document.getElementByID(), is this necessary? Why not simply elementID.?

Richard.
0
 
LVL 3

Expert Comment

by:Sasho
Comment Utility
Sorry RQ I read many of the initial comments and they seemed to be heading in a different direction, with multiple files, I did not notice your post.

Indeed my implementation is very similar to what you posted.



0
 
LVL 40

Expert Comment

by:RQuadling
Comment Utility
All good answers. Split for all would be nice.
0
 
LVL 3

Expert Comment

by:Sasho
Comment Utility
I agree
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now