Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2724
  • Last Modified:

Run php in background including a "loading" effect

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
ping1234
Asked:
ping1234
  • 7
  • 5
  • 4
  • +4
5 Solutions
 
maundedCommented:
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
 
ping1234Author Commented:
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
 
maundedCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
ping1234Author Commented:
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
 
maundedCommented:
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
 
ping1234Author Commented:
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
 
ZylochCommented:
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
 
fetchmasterCommented:
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
 
ping1234Author Commented:
the reload part is ok..I just can't get the "loading" effect that I expected..
0
 
ZylochCommented:
The main window is having the loading effect?
0
 
ping1234Author Commented:
no, the popup one..please check pervious message..
0
 
ZylochCommented:
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
 
maundedCommented:
Does the page load at all before it runs the query?
0
 
ping1234Author Commented:
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
 
ZylochCommented:
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
 
maundedCommented:
I output the html before doing anything in PHP
0
 
ping1234Author Commented:
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
 
Richard QuadlingSenior Software DeverloperCommented:
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
 
m1ckey_GCommented:
Yo can try using output control.

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

// Your query here
0
 
SashoCommented:
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
 
SashoCommented:
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
 
fetchmasterCommented:
Sasho,


<div id='Good' 'display:none;z-index:200'>
should be
<div id='Good' style='display:none;z-index:200'>
i think?
0
 
Richard QuadlingSenior Software DeverloperCommented:
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
 
SashoCommented:
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
 
Richard QuadlingSenior Software DeverloperCommented:
All good answers. Split for all would be nice.
0
 
SashoCommented:
I agree
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 7
  • 5
  • 4
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now