Web Analytics Made Easy -
StatCounter multi step forms...switching steps? - CodingForum

Announcement

Collapse
No announcement yet.

multi step forms...switching steps?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • multi step forms...switching steps?

    Looking through posts here and on some other forums/sites 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.

  • #2
    For validation, you should always validate variables within your PHP script
    to make sure nothing bad gets into the variables ... but for a better user
    experience ALSO validate using some Javascripting, directly on the form page.
    The gives the user immediate feedback on any mistakes, like a bad formatted
    email address, entering a password twice, etc.

    There are several Javascript and AJAX type form validations you can use
    directly on your form page(s) ... use Google to find some examples.

    Part 2 ...

    There are two ways to do this.
    Which method really depends on the purpose of your form and how the info is used ...

    1) Only use SESSIONS to save variables and they are retained until the
    user closes their browser.

    2) You use some sort of MySQL registration thing where a user creates an id like
    an email address and password. They can then go in and edit their profile on a
    profile page (at their leisure) ... if they leave and come back later, they simply log
    back in and continue. You (as administrator) can view any user's profile and they
    are always kept stored in the database.


    Method 1 requires you to assign the session variables within your form ...
    As you enter the "next form", you save the previous form variables as
    session variables in the event you need to go back ... and if you do go
    back, you will have them already put into the form.

    Name: <input type="text" name="username" value="<?=$_SESSION['username']?>">
    If that variable had been previously saved, it would appear as the value,
    otherwise, they simply don't show anything.

    There are some limitations to this ... there is a limit to session time. If they sit
    and take too long to fill out the form pages, their session will be lost. Check your
    webhost to find out the time limit. Also check for session size limit.

    You could also use cookies.

    Use Google to find scripts that use sessions (like a shopping cart script).
    You would basically be doing the same kind of thing.
    Last edited by mlseim; Apr 11, 2009, 11:43 AM.

    Comment


    • #3
      I would recommend a single page for all the steps/forms, using a 'step' session variable to determine at what point you are at. If there are any validation errors, you just stay on the same step and redisplay the form that corresponds to that step with the existing data. If there are no validation errors, you increment the step variable which causes the form that corresponds to the next step to be displayed.
      Finding out HOW to do something is called research, i.e. keep searching until you find the answer. After you attempt to do something and cannot solve a problem with it yourself, would be when you ask others for help.

      Comment


      • #4
        you could do the second step like this :

        on form page:
        PHP Code:
        <?php
        session_start
        ();
        if(
        $_SESSION['test']="true"){
        echo 
        $_SESSION['errmsg'];
        ?>
        <form method="post" target="submit.php">
        <input type="text" name="name" value="<?php echo $_SESSION['name']; ?>"><br />
        <input type="text" name="age" value="<?php echo $_SESSION['age']; ?>"><br />
        <input type="text" name="email" value="<?php echo $_SESSION['email']; ?>"><br/>
        <input type="submit" value="Send"><br/>
        </form>
        <?php
        }
        else{
        ?> 
        <form method="post" target="submit.php">
        <input type="text" name="name" value="Name"><br />
        <input type="text" name="age" value="Age"><br />
        <input type="text" name="email" value="E-mail"><br/>
        <input type="submit" value="Send"><br/>
        </form>
        <?php
        }
        ?>
        Submit page:
        PHP Code:
        <?php
        session_start
        ();
        YOUR SCRIPT FOR VALIDATING FORM HERE)
        if(
        $validationtest="false"){
        $_SESSION['errmsg'] = "Something went wrong please refill the empty fields";
        $_SESSION['test'] ="true";
        if(
        $ageevalidation="true"){
        $_SESSION['name'] = $_POST['age'];
        }
        if(
        $emailevalidation="true"){
        $_SESSION['name'] = $_POST['email'];
        }
        if(
        $nameevalidation="true"){
        $_SESSION['name'] = $_POST['name'];
        }
        header'Location: form.php' ) ;
        }
        else{
        echo 
        "the form was submitted successfull";
        }

        Comment

        Working...
        X