Best Way To Control Forms

From: Date: Fri, 23 Feb 2001 00:23:10 +0000
Subject: Best Way To Control Forms
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-41245@lists.php.net to get a copy of this message
I have a few sites which contain forms that span multiple screens (all contained in two scripts though...) theforms.php <? // Include file class theForms { function showScreen1() { global $error; ?> The html for the form. <? if($error){ echo $error; } ?> <? }//function showScreen1 function saveScreen1() { global $HTTP_POST_VARS; // save the input into a database or file etc. if(success) { return true; } else { return false; } }//function saveScreen1 function showScreen2() { global $error; ?> The html for the form. <? if($error){ echo $error; } ?> <? }//function showScreen1 function saveScreen2() { global $HTTP_POST_VARS; // save the input into a database or file etc. if(success) { return true; } else { return false; } }//function saveScreen1 } ?> forms.php <? include('theforms.php'); $theForms = new theForms; if(!isset($screen)){ $screen = 'showScreen1'; } switch($screen) { case 'showScreen1': $theForms->showScreen1(); break; case 'saveScreen1': if($theForms->saveScreen1() != true) { $error = "it died!"; $theForms->showScreen1(); break; } case 'showScreen2': $theForms->showScreen2(); break; }//Switch ?> so basically I put all the html into functions in a class, and control the flow of the form with the switch statement, that way if my function that saves the data comes across an error, it get's trapped and shows that screen again... Has anybody else found a more elegant way to accomplish this? It works quite nice, but I was wondering how other people go about creating their forms. Thanks, -Jonathan

« previous php.general (#41245) next »