ds111 Posted January 3, 2009 Share Posted January 3, 2009 So basically If i have a runtime script (which means it gets executed as it goes) and it outputs a bit of html and stuff but then reaches an error. I want it to clear the screen, make it completely blank and echo the html with the error. Is this possible with document.write? Or should i use something in php? die() basically does the trick but what happens is that it just stops the script for executing further, wich means everything that was already outputted is still there, and it looks like a terribly broken design because the rest of code isnt there.... any way to do this in php or javascript? thanks so much! Quote Link to comment https://forums.phpfreaks.com/topic/139366-documentwrite-question/ Share on other sites More sharing options...
bubbasheeko Posted January 3, 2009 Share Posted January 3, 2009 Hi ds111, You can try one of the following two options: document.body.innerHTML="" or location.replace("about:blank") That's a start, you should be able to use document.write to put the error or whatever you want on the screen from there. Quote Link to comment https://forums.phpfreaks.com/topic/139366-documentwrite-question/#findComment-728929 Share on other sites More sharing options...
ds111 Posted January 3, 2009 Author Share Posted January 3, 2009 hmm well the second one made it completely blank but it takes u of the site to a generic blank page, so i cant put any error there.... the first one, erm, sorta works? but i get this however: and document.write not work after that. Code i am using in function: <script> document.body.innerHTML=""; document.write("<?php echo $error; ?>"); </script> Quote Link to comment https://forums.phpfreaks.com/topic/139366-documentwrite-question/#findComment-728934 Share on other sites More sharing options...
KevinM1 Posted January 3, 2009 Share Posted January 3, 2009 I think you'd be better off looking at the design of your site as a whole. Well-designed PHP sites tend to save rendering output to the end, so any errors can be caught and printed to the screen if necessary, in place of the actual site output. In other words: 1. Process your data (this includes obtaining and handling form data) 2. Is there an error? Yes - Print error message No - Render site No JavaScript is needed in that kind of setup, and you won't have half a site created and output to the screen before you run into an error. Quote Link to comment https://forums.phpfreaks.com/topic/139366-documentwrite-question/#findComment-728938 Share on other sites More sharing options...
ds111 Posted January 3, 2009 Author Share Posted January 3, 2009 alright then how do i do that? use ob_start(); at the beginning that at the end say ob_get_contents(); ?? and that way i can put all the errors and stuff at the beginning? Quote Link to comment https://forums.phpfreaks.com/topic/139366-documentwrite-question/#findComment-728940 Share on other sites More sharing options...
KevinM1 Posted January 3, 2009 Share Posted January 3, 2009 alright then how do i do that? use ob_start(); at the beginning that at the end say ob_get_contents(); ?? and that way i can put all the errors and stuff at the beginning? Well, the simplest way is to do something like: $errors = ""; if(/* some condition */) { //something good } else { //error $errors += "Error message 1<br />"; } /* ... */ if(!is_empty($errors)) //we have errors { echo $errors; } else { echo $goodOutput; } In essence, you want to build two output variables as your script runs. One for the good output - the site itself - and one for the errors. If any errors exist, then those should be output instead of the site. You don't have to store the outputs as string variables. You can use associative arrays, or objects, to keep track of everything. Output buffering could work, too, but it may get a bit messy considering there's two output streams that need to be accounted for. Quote Link to comment https://forums.phpfreaks.com/topic/139366-documentwrite-question/#findComment-728948 Share on other sites More sharing options...
ds111 Posted January 3, 2009 Author Share Posted January 3, 2009 well actually what i was thinking was... ob_start(); //All the php code if(!isset($query)) { /* An example of a real error handling i have */ $this->handle("query_fail"); } // Code of query_fail: ob_end_clean(); ob_start(); echo "Query failed..."; ob_flush(); Would that work? Edit: tried it on site, yes it does! WAHOO! Quote Link to comment https://forums.phpfreaks.com/topic/139366-documentwrite-question/#findComment-728950 Share on other sites More sharing options...
KevinM1 Posted January 3, 2009 Share Posted January 3, 2009 well actually what i was thinking was... ob_start(); //All the php code if(!isset($query)) { /* An example of a real error handling i have */ $this->handle("query_fail"); } // Code of query_fail: ob_end_clean(); ob_start(); echo "Query failed..."; ob_flush(); Would that work? It probably will. It's a bit messy, if you're doing things the way I think you're doing them, but it'll save you time. Just be sure to die() after each error is reached. That way you won't have cascading errors on the screen (a failed query leads to another error, which leads to another, etc). Quote Link to comment https://forums.phpfreaks.com/topic/139366-documentwrite-question/#findComment-728955 Share on other sites More sharing options...
ds111 Posted January 3, 2009 Author Share Posted January 3, 2009 hilarious u would say that because when i refreshed the page with the output buffers, i got cascading errors. then i did a "exit;" after my error handler function and that fixed it...still funny how u called it Quote Link to comment https://forums.phpfreaks.com/topic/139366-documentwrite-question/#findComment-728979 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.