Jump to content

Document.Write question


ds111

Recommended Posts

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!

Link to comment
Share on other sites

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:

 

picture1sg5.png

 

and document.write not work after that. Code i am using in function:

 


<script>
	document.body.innerHTML="";

	document.write("<?php echo $error; ?>");
	</script>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.