Page 1 of 1

multiple stage, self processing forms in a single file

Posted: Mon Jun 02, 2003 3:37 pm
by m3rajk
i guess i'll start witha hello and a short explanation of why i'm here.

hi everyone. i'm the coder for a site in the works. two friends of mine are doing graphics, i'm doing code. i'm hoping to use the site as a way to get around the catch-22 i experienced when trying to get internships in college (see my signature).

right now i'm trying to get everything working without the database (ie: no memory on the site) so that i know the php is working and just needs modification. the problem is that my forms refuse to work correctly. i have 4 stages i want to roll thgrough, eeach one checking the one before it was done, or sending the user back to the previous one with the errors shown.

my problem is that it's not displaying correctly. my code looks something like:

Code: Select all

if(isset($stage)&&($stage=='2')){
  /* check for errors in the previous stage. if found set $stage to previous stage and $errors to errors found */
}elseif(isset($stage)&&($stage=='3')){
  /* check for errors in the previous stage. if found set $stage to previous stage and $errors to errors found */
}

if(isset($stage)&&($stage=='1')&&($error==NULL)){
  /* display page one */
}elseif(isset($stage)&&($stage=='1')&&($error!=NULL)){
  /* display page one with errors */
}elseif(isset($stage)&&($stage=='2')&&($error==NULL)){
  /* display page two */
}elseif(isset($stage)&&($stage=='2')&&($error!=NULL)){
  /* display page 2 with errors */
}elseif(isset($stage)&&($stage=='3')&&($error==NULL)){
  /* display page three */ 
}else{
  /* display general error */
}
obviously i have actual working pages set up, but the combined page for all of this displays the first part and part of 2 and 3 telling me it cannot find things that are not set because that stage has not been gotten to yet.

i don't understand why it's even moving beyond the first page the first time.

Posted: Mon Jun 02, 2003 4:19 pm
by CyberSpatium
in your code above, do not use quotes for contstant values:

you used:

Code: Select all

if(isset($stage)&&($stage=='2')){
Use this without quotes:

Code: Select all

if(isset($stage)&&($stage==2)) {
you do not need to use quotes for varibables ($variable) or numbers. you do need to use quotes on most everything else ('some text') for example.

you are missing an extra = sign here. change this:

Code: Select all

}elseif(isset($stage)&&($stage=='1')&&($error!=NULL)){
to:

Code: Select all

} elseif (isset($stage) && ($stage == 1) && ($error !== NULL)){
also, you might try changing:

Code: Select all

if(isset($stage)&&($stage=='1')&&($error==NULL)){
to this (use ' ' instead of NULL):

Code: Select all

if(isset($stage)&&($stage==1)&&($error==' ')){
Here is your code fixed and with much less redundancy:

Code: Select all

if (isset($stage)) {

	if ($stage == 2) { 
		# check for errors in the previous stage. if found set $stage to previous stage and $errors to errors found 
	} elseif ($stage == 3)){ 
		# check for errors in the previous stage. if found set $stage to previous stage and $errors to errors found
	} elseif (($stage == 1) && ($error == '')) { 
		# display page one */ 
	} elseif (($stage == 1) && ($error !== '')) { 
		# display page one with errors
	} elseif (($stage == 2) && ($error == '')) { 
		# display page two
	} elseif (($stage == 2) && ($error !=='')) { 
		# display page 2 with errors
	} elseif (($stage == 3) && ($error == '')) { 
		# display page three
	}else{ 
		# display general error
	}
}

Posted: Mon Jun 02, 2003 8:38 pm
by m3rajk
thank you. it now works with the first stage and seems to execute correctly, but it doesn't move on (better than before. it used to call it broken, now it seems to move on but displays a blank page instead of the second stage)

Posted: Tue Jun 03, 2003 2:17 am
by discobean
it looks incredibly painful..

I would have broken up the logic of the error codes, and the current page/stage eg..

Code: Select all

switch($stage)
{
  case 1 :
    if($error) { /* show error stuff */ }
    else { /* show stage stuff */ }
  break;

  default :
    /* default page goes here */
  break;
}
this is much much neater, and far more manageable instead of a heap of nested if's..

Marz.