SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Form processing in two pages

    Hi,

    I have a very long form which I want to separate it into two or three parts/pages. Can anyone tell me how would I do that? Divide the forms into two pages is not the main problem, the main concern is how am I going to process the form datas in both pages and send via email in one mail. Am I sound make sense? I came across some other sites where you fill in one page for step 1 and then proceed into the next steps and so on.....and then click submit to send the two or multiple pages form data to one email. Any comments greatly appreciated, thanks

    Please note, I am not neither a php expert or programmer. Please help.

  2. #2
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just split your form into two pages. When you click to submit first page/form (step 1) then you will be posted to second page which contains your second form. Here you can just save the data of first form either in the Sessions or you can simply store them in some HIDDEN form elements. I would prefer to store the data in Hidden elements because easier validations and data will not be lost if the second page is submitted for a long time. If the second page is not submitted for a long time then the session might be lost.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  3. #3
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    153 Post(s)
    Tagged
    3 Thread(s)
    Quick 'n' Dirty multipage form tutorial.....

    Page 1 of the form:

    page1.php
    PHP Code:
    <?php
    // start a new session
    session_start();

    // check if the form has been submitted by using
    // a hidden field - NOT the submit button!
    if(isset($_POST['formsubmit:page_1'])) {
        
    // iterate through all the posted variables
        // and register them in the session called 
        // formdata
        // You can do validation and strip out the submit button value 
        // here.
        
    foreach($_POST as $data) {
            
    $_SESSION['formdata'][] = $data;
        }
        
    // redirect the user to the next page....
        
    header("Location: page_2.php");
        exit();
    }

    ?>
    <form name="myForm" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" />
        <label>Name</label>
            <input type="text" name="your_name" value="" /><br />
        <label>Country</label>    
            <input type="text" name="your_country" value="" /><br />
            <input type="submit" name="submit" value="go to page 2" />
            <input type="hidden" name="formsubmit:page_1" value="1" />
    </form>
    page 2:
    page_2.php
    PHP Code:
    <?php
    // start a new session
    session_start();

    if(isset(
    $_POST['formsubmit:page_2'])) {
        
    // same as before
        
    foreach($_POST as $data) {
            
    $_SESSION['formdata'][] = $data;
        }
        
    // instead of redirecting the user, this time we will
        // dump out the contents of the session variable
        // to check it's contents
        #header("Location: page_3.php");
        
    echo '<pre>';
        
    print_r($_SESSION);
        echo 
    '</pre>';
    }

    ?>
    <form name="myForm" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" />
        <label>Page 2 Name</label>
            <input type="text" name="your_other_name" value="" /><br />
        <label>Page 2 Country</label>    
            <input type="text" name="your_other_country" value="" /><br />
            <input type="submit" name="submit" value="Check" />
            <input type="hidden" name="formsubmit:page_2" value="1" />
    </form>
    Create the 2 pages and run them on the server. You should end up with formatted output on page 2 after you have submitted the form.

    Code:
    Array
    (
        [formdata] => Array
            (
                [0] => spike
                [1] => england
                [2] => go to page 2
                [3] => 1
                [4] => P2 Spike
                [5] => Ps England
                [6] => Check
                [7] => 1
            )
    
    )
    Have a go and post back if you need help or further explanation.
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  4. #4
    SitePoint Wizard Hammer65's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln Nebraska
    Posts
    1,161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Store each submission in the session first. When the last page has been submitted, get everything from the session array variable that you used to store everything and format your email with that data.

    Something that does multipage forms, is a pretty good "toolbox" item. Meaning, if you develop code for such a thing, you should make it as generic and re-usable as possible. It should act much like a "controller" class in an MVC application so that you can use it for any project.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •