Solved

Run php in background including a "loading" effect

Posted on 2004-09-13
29
2,709 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
ID: 12042154
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
ID: 12042182
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
ID: 12042246
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
ID: 12042345
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
ID: 12042388
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
ID: 12042428
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
ID: 12042439
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
ID: 12042449
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
ID: 12042472
the reload part is ok..I just can't get the "loading" effect that I expected..
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 12042478
The main window is having the loading effect?
0
 

Author Comment

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

Expert Comment

by:Zyloch
ID: 12042521
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
ID: 12042523
Does the page load at all before it runs the query?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:ping1234
ID: 12042544
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
ID: 12042566
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
ID: 12042569
I output the html before doing anything in PHP
0
 

Author Comment

by:ping1234
ID: 12042725
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
ID: 12043270
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
ID: 12043701
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
ID: 12049927
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
ID: 12049979
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
ID: 12051335
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
ID: 12051868
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
ID: 12055910
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
ID: 12492422
All good answers. Split for all would be nice.
0
 
LVL 3

Expert Comment

by:Sasho
ID: 12494407
I agree
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to count occurrences of each item in an array.

863 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

22 Experts available now in Live!

Get 1:1 Help Now