Jump to content

more multiple part forms...


tefuzz

Recommended Posts

Looking through posts here I have made my way into creating a multiple part form...With some help on code duplication I switched over to using functions to display my form fields, which makes looking at it 100% better. My entire first page of my form only has 66 lines of code instead of the almost 400 I had before...Now comes my dilemma.

 

Sessions seem to be the way to go. I have questions though...I will have 6 parts all together: 4 forms (each on their own page), 1 review/submit page, and 1 thank you page. Where I am having the trouble is the "order" of doing things, and exactly how to accomplish what I am looking for.

 

1. I would like to validate each step as the 'next' button is pressed, if errors are found I want the page to reload with the good fields filled in and the error fields marked for errors. Should I have a separate "processing" page which will check which step the user is on, and validate the fields accordingly?

 

2. With the session, if the user hits the back button does the session keep the variables intact? And is there a way to repopulate the fields that were entered?

 

I'll start with those 2 questions. I have already created "step 1" and I am ready to begin the "step 2" but not sure where to start.

Link to comment
Share on other sites

I managed to create a multi-part form without needing to use sessions, all I used was functions. I have also got it to validate each part of the form upon submisssion, by using an errors array. If errors are found in the form input, the errors array is populated with the field name that has the error in it, then that form is redispalyed with the errors shown:

 

if(!empty($errors))

{

form1($errors);

}

else

{

form2($_POST);

}

Link to comment
Share on other sites

 
if($_SERVER['REQUEST_METHOD'] != 'POST')
{
  form1($_POST); //use $_POST for the sake of providing an array to use
}

else if($_POST['process'] == 1)
{
  if(strlen($_POST['name']) < 2 || trim($_POST['name']) == '')
  {
   $errors[]='name';
  }
  
  if(!empty($errors))
  {
   form1($errors);
  }
  else
  {
   form2($_POST);
  }
}

else if($_POST['process'] == 2)
{

// form 2 processing here

}

 

On your form have a hidden input field called "process" with value of "1" on your first form, and value of "2 on your second form, and so on.

Link to comment
Share on other sites

function form1($values)
{

<input class="text" name="firstname" ', in_array(firstname, $values) ? ' style="background-color: #FFEACE"' : null ,' type="text" value="', isset($_POST[firstname]) ? $_POST[firstname] : null ,'" size="10"/>

}

Link to comment
Share on other sites

The input field is checking if 'firstname' is in the array $values.

 

$values will either be a blank array or will contain the $errors array that will get passed in if there are any errors found.

 

If 'firstname' is found in the array, it sets the background of that field to a certain colour, to indicate there was a problem with that field.

Link to comment
Share on other sites

personally I'd have the whole thing on one form and use javascript to control which bits are displayed at each stage; all you need are back/next buttons implemented in javascript and you have a fantastic user experience with all the code in one one place and easy to manage. this would cut down the number of requests made to the server - you could use some ajax to validate each stage and all in all be an awesome form to use.

Link to comment
Share on other sites

Ok, I think im getting closer to understanding this...

 

so this piece of code


else if($_POST['process'] == 1)
{
  if(strlen($_POST['name']) < 2 || trim($_POST['name']) == '')
  {
   $errors[]='name';
  }

  if(!empty($errors))
  {
   form1($errors);
  }

 

Is First checking to see if "process" is "1".

If it IS, then it will begin checking the fields for errors.

If name has an error, it adds name to the $errors array

 

then, after the fields are validated, if there are errors found, it runs the form() function passing through the $errors array. right so far?

Link to comment
Share on other sites

@omzy - you ALWAYS validate server side anyway you should NEVER leave validation to javascript. AJAX is different as you can validate server side 'on-the-fly' - this just removes some of the frustration the user could have if they fill out lots of info and have to do some again after hitting submit.

Link to comment
Share on other sites

and now we've called the form function, and passed trough $errors array as $values.

 

function form1($values)
{

<input class="text" name="firstname" ', in_array(firstname, $values) ? ' style="background-color: #FFEACE"' : null ,' type="text" value="', isset($_POST[firstname]) ? $_POST[firstname] : null ,'" size="10"/>

}

 

so, the function is going to display the input field, this piece of it:

in_array(firstname, $values) ? ' style="background-color: #FFEACE"' : null

 

is checking the array, and if firstname is found (referred to as name before, but i know what you meant) '?' thats a shortened 'if' right? so if returns that firstname is present, it will change the background color ( : being the else?) it will return nothing?

 

am i seeing this right?

 

and it does the same for the value of the field, checking if the $_POST was set with this:

isset($_POST[firstname]) ? $_POST[firstname] : null ,

 

did i get it all? :D

Link to comment
Share on other sites

Yep - AJAX (Asynchronous JavaScript and XML)

 

But yep, each to their own ;-)

 

I had looked at an AJAX approach, but I figured I'd stick to learning one language at a time...last time i built a website was before CSS came around...only dabbled in since then, and now I'm getting back into it  ;D

 

thanks for all the help omzy. i'm going to leave this topic as unsolved until I get it working, that way I can post in here if I have problems (dont forget about me ;))

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.