[SOLVED] Detecting if the user hit to the Stop button

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

[SOLVED] Detecting if the user hit to the Stop button

Post by anjanesh »

When a page has finished loading I can use the onload event in body to call some function.
For a process that takes time, say 5 min, what if someone clicks the stop button and I still want to trigger that onload JS function ? Anyway to get that to work ?

Code: Select all

<body onload="alert('done');">
<?
loop
{
    // Do something
    flush();    
}
?>
</body>
How can I detect when the stop button has been clicked in the browser ?
Thanks
Last edited by anjanesh on Tue Mar 29, 2005 5:18 am, edited 1 time in total.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

As far as I know/been told in the past there is nothing you can do. Stop is just that. If you could intercept this and then do things too many people would start to "disable" it.

In reality it is down to the web page design. Show something quickly if necessary. Give them a warning it may take time to process. Give messages telling them it is 10%,90% complete or whatever.

You have two time delays. PHP processing time, which should never really take that long and the time it actually takes to download (Modem speed being key here). 5 minutes php processing time is really too long for users, unless they get messages telling them something is happening. You could potentially have a waiting status bar with a Cancel process link which would link to another page informing the user that the process was cancelled. Give the user other options and they "should" ignore the stop button as their eyes are normally on your web page not the toolbar.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Thanks for the info. Actually this is not really that essential but I thought there was a way.

Code: Select all

<?
 echo '<table>';
 Loop
 {
 	echo '<tr><td>'.display().'</td></tr>';
 	flush();
 }
 echo '</table>';
?>
If Someone hits the Stop button the closing </table> tag never gets printed which is ok for a page php processes fast.
What I have is that it exracts some info from some site and displays one by one. This takes time even on servers - 2 reasons - file_get_contents and preg_match
preg_match really take time.

Thanks anyway
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Could you use CSS rather than a table as an alternative ?
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Till date I've only associated CSS with html tags.
How do you remove table/div tag and repalce it with CSS ?

Btw, The table would be in between <form></form> to perform some action against those rows checked.
So even the last </form> would not be printed.
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

This simple snip does the trick :wink:

Code: Select all

function showMSG()
{ alert(&quote;Why did you press the bloody STOP Button! \r\n Can't Ya Wait for it to load ?&quote;) }
document.onstop = showMSG;
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Where do I put this document.onstop = showMSG; ?
Its not working in <body onstop="alert('stopped')">
Not working in IE 6 too.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

I am not suggesting you replace the DIV tags. The key to CSS is to remember you are separating content from format. CSS handles the style or format. The content needs to be "Tagged" to recognise the structure which is where HTML comes in. Tables unfortunately are often in the middle ground. I am not someone who says all tables should be transferred to CSS. Tables do have their place in HTML and content although not always as they are currently used, notably to format columns into columns. (Quick note here I come from a paper documentation background including SGML/XML).

For an example of a tableless design you may want to look at the following article.

[url]ttp://www.alistapart.com/articles/practicalcss/[/url]

I am sure there are many other articles around.
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

Put it in the head section of the page. whole of it.
your snip doesn't work because onstop then gets associated with document.body object. ;)
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Well n00b Saibot's code is a somthing new I've learnt but a couple of points to bear in mind...

1) What if javascript is not active ?
2) Still does not close the table (Although I suppose you could use javascript to close the tag but IMO you are starting to make things messy).
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

Yah! dat's the prob with JS. people can turn it off! I hate this feature :x I know it is for our own good but knowing there is a functionality/feature that will do our job easily and a great one too and not able to fully rely on its existence. Bah! makes me feel grrrr...
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Thanks. It worked.
IE only ! But atleast the majority of the users will get it.
CoderGoblin wrote:Still does not close the table (Although I suppose you could use javascript to close the tag but IMO you are starting to make things messy).
Is there any other way out other than document.write("</table>"); in that onstop function ?
We will need one flush_on_stop() function in PHP.
And thanks for that CSS article.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Not as far as I know (but then I have been wrong before). You also need to check if the table has been started... What happens if the user pushes stop before the table is opened ? Shouldn't happen but users being users....
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

Nah! I don't think there is anything else than document.write that will be helpful in your case.

If there comes any other prob. in completing it, sure tell me. :wink:
Post Reply