FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Stopping the script without forcing the page to quit loading





Marston
I notice that when I exit() a script, (or die() a script, for that matter), it forces the page to stop loading. Is there any workaround for this?
hexkid
Marston wrote:
I notice that when I exit() a script, (or die() a script, for that matter), it forces the page to stop loading. Is there any workaround for this?


Instead of
Code:
<?php
if ($input == '') {
  exit('No input. Try again.');
}
echo 'rest of script</body></html>';
?>
do
Code:
<?php
$exit_msg = false;
if ($input == '') {
  $exit_msg = 'No input. Try again';
}
echo 'rest of script';
if ($exit_msg !== false) {
  echo $exit_msg;
}
echo '</body></html>';
?>
Marston
I'm not sure if that would work though, because the script is like a registration script, and I need the script to terminate in the case of an error, but I would like the rest of the page to load.

Like, if you forget to fill something in and hit "submit", I need the script to terminate, but I also need the rest of the page to load. Exit and Die just stop the page in its tracks.
Marston
Well, I guess I figured out a work around - I'll just use JavaScript to redirect to an error page. Not the greatest, but I can work on it later Wink. Thanks for the help anyways, hexkid.
snowboardalliance
Well my registration script will first validate all info. Then it sees if any validations threw an error, if they did, it skips the registration and displays the error. If there are no problems it does the sql query to add the user and tells them it worked.
ChrisCh
Marston wrote:
Well, I guess I figured out a work around - I'll just use JavaScript to redirect to an error page. Not the greatest, but I can work on it later Wink. Thanks for the help anyways, hexkid.

Just be careful when using JavaScript for important things like page redirection - some browsers do not support it and some people may have it disabled, thus meaning that your website will not function properly to those people. Smile
Marston
ChrisCh wrote:
Marston wrote:
Well, I guess I figured out a work around - I'll just use JavaScript to redirect to an error page. Not the greatest, but I can work on it later Wink. Thanks for the help anyways, hexkid.

Just be careful when using JavaScript for important things like page redirection - some browsers do not support it and some people may have it disabled, thus meaning that your website will not function properly to those people. Smile
Too bad for those people Wink
DanielXP
lol thats nice Razz
DSitC
I would recomend to use a function for your registration subprocess. In this case you can cancel the whole process by calling a return(false) or whatever.

I would also recommend to use ob_start() at the beginning of your script and ob_end_flush() at the end. This way you output is buffered and sent at the end of processing which will result in a cleaner behavior in some cases, and will further help you to handle errors properly.
Marston
DanielXP wrote:
lol thats nice Razz
Razz Razz Razz
AftershockVibe
It is possible to call another function after exit() has been called, you can set it using register_shutdown_function(). Have a look here;
http://uk.php.net/manual/en/function.register-shutdown-function.php

So you could define a function which outputs the remainder of a page, presumably the ends of </div>s and footer stuff.

However, if you don't know where your script is going to end exactly when outputting the HTML then this becomes very difficult to get right.

It is more suitable for getting rid of cookies and clearing variable clutter after an error really.

Cool
Related topics
Do you use css to design a website?
Basic HTML Introduction
php without server
Keywords script for your site! Works on frihost sites!
I need a loading bar that appears while (read inside)
Creating a page with php
PHP Image Questions
comment integrer un script dans une page php
navigation system probs
java script checking page
Automaticly Submit Post Data. 20 fri$ up for grabs.
Running PHP Script without PHP Server?
PHP Whos Online Code OR Page refresh..
SPLIT long task?
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.