Jump to content

multi-step form - one file.


eche

Recommended Posts

Yes I know what you're thinking, not another really basic question that you can answer. But I'll be honest with you, I did try searching and the results are less than what I wanted. So let me try and explain what I have done and what I am trying to do!

 

Here is what is involved:

1) The form is a multi step "contact us" process, so depending on the option, something else displays and so on.

2) Validation on the server-side, I do not want to use javascript as it presents problems (turned off on the client side - I know, highly unlikely, but you never know)

3) One the user gets through the certain steps, anywhere from 3-5 steps, they are presented a form which they fill out (personal details, questions etc - being a contact us form) and then submit. Processing of this data is no issue, it is sent to a specific email address - simple.

 

Now what I have done so far is create the individual "pages" if you like, every possible option that a user can be presented with. I have also worked out the validation for each, separately they work like a charm, putting it all together is an absolute nightmare.

 

What I want it to do is to work off one page or file. So if I had the address http://www.domain.com/contact.php .. as the user is working through the steps, they are submitting to the same file. I don't want (the easier option, I know) individual files, such as step1, step2, or red, blue, green etc.

 

So when it came time for me to put things together, I ended up with a long list of if, else if statements that in the end simply did not work. I tried using a switch/case setup, only issue with this was the validations were not working. I looked at sessions, but fell short of understanding how to display a certain step without employing an if statement or 3. Had this have been dos, I would have probably used goto, it's not an option here however.

 

I also know there is a solution using classes, I search in here and google and found it. But I would prefer a "home-made" solution.

 

So my question is what am I missing here (and please, don't say everything, that's just as helpful as saying f* off). I want it to work the way I intended, a multistep contact form, no javascript, server side validations and a submission at the end to an email. It sounds simple.

 

Did I mention I've pulled some of my hair out? ???

 

The funny thing is I will probably look back on this in a couple of months time and realize what an idiot I really am.

Link to comment
Share on other sites

You're going to need to use sessions to store the data unless you want to store it in the GET request which is not a good idea. Store the current step that the person is on in the GET request however. Using if and else if statements to decide which form to display and which set of validation to use depending on what step they are on is what i would use personally.

Link to comment
Share on other sites

I'd use post, generating <input type="hidden" /> to pass the value between steps.

Don't use this for banknummers and things like that however! Do those in the last step when submitting directly to the server.

 

You could determine where you are based on either the value given so far (yes it gets complicated) or by making your submits like "Step 4" and relying on that.

Link to comment
Share on other sites

Thanks for your responses so far.

 

I have had a look at sessions, and it's probably the way to go. As for storing values and passing them through hidden fields, I did try this, but I tried it coupled with a switch and that's where things went pear shaped.

 

Let me try and explain my form. Here's a link to a flow chart (quickly knocked up, not really an artist) to better explain the process. At least I think it does. http://i44.tinypic.com/2ywshs4.jpg

 

That is the way I would like to work. So for it to happen that way, I need to know what step they are up to plus whether they have filled it in (validation side). This is what I have had thus far (excuse the syntax):

 

if !isset(submit) {
show first step
} else {
if step = 2 {
show step 2
} elseif step = 3 {
show step 3
... etc ...
} else {
show step 1 with error
}

 

Showing a particular step, I have a switch/case system. So step 2 corresponds to the case 2 for example.

 

Now the problem with all of that is the validation. How or where do I fit the validation.

 

Or am I going about this the wrong way with that type of system I have there?

 

Link to comment
Share on other sites

Like I said AJAX is your ideal solution, using AJAX with sessions even better. So what I think you should do is make a php function for the kind of validation and protection that will be applied to each form var and at each step. Then on each step call the function and preform your next step. To keep it too one page that is where AJAX's comes in, as the user fills in information it will submit it for validation, each submit moves you along your switch statement and or increments your session counter you can even base your switch by which POST var is being submitted by AJAX. Once they have completed the form you will have already validated and accepted the information, with out them having hit submit. when they submit the form you can check the final variable and compare the variables recieved agianst the variables previously recieved to make sure they did not change something, and AJAX was not able to catch it.

Any way that you go about it, your solution will have to be complex in order to ensure security and validation, but it can be done. Also once you get it working you will no doubt find ways to trim the code down.

Link to comment
Share on other sites

 

 

here a simple example.

<?php session_start(); ?>

<?php

if($_GET['cmd']=="step4"){

$country=$_POST['country'];

$_SESSION['country']=$country;

echo "
Your name is: {$_SESSION['name']}\n <br>
Your age is: {$_SESSION['age']}\n <br>
Your country is: {$_SESSION['country']}\n ";

exit;

}
?>

<?php

if($_GET['cmd']=="step3"){

$age=$_POST['age'];

$_SESSION['age']=$age;
?>

<form method="POST" action="<?php $_SERVER['PHP_SELF']?>?cmd=step4">

please enter your country

<br><br>

<input type="text" name="country" value="<?php echo $_SESSION['country'];?>">

<br><br>

<input type="submit" name="submit" value="continue to results!">

</form>

<?php

   exit;

}
?>


<?php

if($_GET['cmd']=="step2"){

$name=$_POST['name'];

$_SESSION['name']=$name;
?>

<form method="POST" action="<?php $_SERVER['PHP_SELF']?>?cmd=step3">

please enter your age

<br><br>

<input type="text" name="age" value="<?php echo $_SESSION['age'];?>">

<br><br>

<input type="submit" name="submit" value="continue with quistion 3">

</form>

<?php

   exit;

}
?>


<form method="POST" action="<?php $_SERVER['PHP_SELF']?>?cmd=step2">

please enter your name

<br><br>

<input type="text" name="name" value="<?php echo $_SESSION['name'];?>">

<br><br>

<input type="submit" name="submit" value="continue with quistion 2">

</form>

Link to comment
Share on other sites

in one of the webapps i'm converting to php i use a wizard-like UI for report generation. single page. the user can move forward and back, etc. since the data isn't secure in nature and it's composed of only a few elements, i chose to maintain state in the form itself using hidden elements.

 

the first thing you want to do is determine the current step. if the user hit the cancel button, go to the home page. collect the current state from the form elements. if the user hit the back button, go to the previous step. if the user hit the next button, go to the next step. if the user hit the finish button, display the results.

 

amongst the html i have a bit of php to determine the current step and the form elements to display and hide.

 

jason

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.