Page 1 of 1

Output Buffers? How can I fix the layout? :(

Posted: Wed Aug 21, 2002 3:41 pm
by JPlush76
ok I have a webform that users submit info on and when they click submit it takes them to a form that processes the request.

on that form I have a simple check to make sure they filled out the right fields

Code: Select all

//***** CHEST FOR INVALID FIELDS

if (!$name || !$email)
{
	echo "Name, Email are Required.<BR><BR>";
	echo " Please click your browser's BACK button so your information is not lost.";
	exit;
&#125;
well if the fields aren't filled in the HTML stops processing after the EXIT command and it leaves half an html page

What I want to do is process all the HTML so the page looks right and then display the php error msg.

From what I read Output buffering is for this:

Code: Select all

ob_start();
that didn't seem to work either, anyone have any ideas?

Posted: Wed Aug 21, 2002 3:56 pm
by nielsene
I can think of several ways to do this, but all will likely require a little bit to a lot of rework on other parts of your script. So I'll throw out several suggestions and you can tell us why they don't work for you :D

1. Don't exit. Return an error string that the content manager displays instead of its regular content if its non-empty

2. Don't exit, redirect back to the orginal form, again passing an error message that you'l display if non-empty. also pass back (via session or get) the entered values so you don't lose them, redisplay them on the form

3. Package up the code you need for the footer / page formatting as a function and call it as needed before exiting.

I use versions of all three on my sites. Methods 1 & 2 are used for most activities. Method 3 is used when I detect "bad guy" activity and want to stop script execution immediately. (I also destroy the session, etc)

Of course on my site bad guy activity could simply be using the back button right now :) as I don't have enough check to make sure resubmitting doesn't cause sql problems, so I just block certain bad page-state transitions.

Posted: Wed Aug 21, 2002 3:59 pm
by JPlush76
ahh I didn't know you could do returns in "if statements"?

Code: Select all

//***** CHEST FOR INVALID FIELDS 

if (!$name || !$email) 
&#123; 
   echo "Name, Email are Required.<BR><BR>"; 
   $erroryes =  " Please click your browser's BACK button so your information is not lost."; 
   return $erroryes; 
&#125;
like that? or do I need to make that error check a function?

Posted: Wed Aug 21, 2002 4:03 pm
by nielsene
Ooops, yeah you can't return from an if... in that case either wrap the error checking in a function or

Code: Select all

$errorMsg="";
//your error code, set $errorMsg if there is an error
// do regular page start formatting
if (""==$errorMsg)
&#123;
   // handling printing errorMsg
&#125;
else
&#123;
   // handle regular content display
&#125;
// do regular page end formatting