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

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
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

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

Post 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?
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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.
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post 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?
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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
Post Reply