Results 1 to 6 of 6

Thread: Multi-Step form posting w/ $PHP_SELF

  1. #1

    Multi-Step form posting w/ $PHP_SELF

    I'm creating a mult-step PHP quote form that is going to be posting to itself, but I've never tried it this way, but needs to be done this way. I'm fundamentally sound with PHP, but have some questions regarding this method. The steps are as follows:

    1. Personal Information
    2. Delivery Information
    3. Product Information
    4. Confirmation > Submission

    I'm comfortable enough with being able to do this in one step, but not more than one. My primary question is, how do I get the form to go to the next step, and so on, after the user has filled out the first step? Would I implement hidden input types? If so, how and where? Any help would be appreciated.

    JPearson311

  2. #2
    actionAction's Avatar
    1,142
    posts
    humanBeing._beard=true;
    Hi,
    There are practically an infinite number of ways this could be accomplished (as I am sure you are aware). You could just test to see if $_POST variables are set and rewrite the HTML based on those factors. Hidden input types might be helpful, but once the variable is set, you don't really need to passing the hidden value anymore.

    The example I give here sets all of your variables to the corresponding form names like you would expect. I also initialize a variable called "step" at 1, and based on what variables are/aren't set, change the value of step (Seems pretty logical right? If name is set and shipping address isn't, step is 2). I then wrote a function called WriteForm() with the step variable as its parameter; it performs a switch statement to decide which form to write. Each form has its own function to echo the appropriate form content e.g. PersonalForm(). I am really more of a C++ person, so I can't really tell you if this is the best way, but with enough tweeking, it should work! Hope it helps! (BTW, this is just a simplified example that probably won't work as is):
    PHP Code:
    <?php

    $name 
    $_POST['Name'];
    $address $_POST['Address'];
    $productInfo $_POST['ProductInfo'];
    #etc

    //Set the default of which step you are on here
    $step 1;

    if(!isset(
    $name))
    {
        
    $step 1;
    }
    elseif(isset(
    $name) && !isset($address))
    {
        
    $step 2;
    }
    elseif(isset(
    $address) && !isset($productInfo))
    {
        
    $step 3;
    }
    else
    {
        
    $step 4;
    }

    //Example form writer
    function PersonalForm()
    {
        echo 
    '<tr><td>Name</td><td><input type="text" name="Name" /></td></tr>';
        echo 
    #other personal stuff
        
    echo '<tr><td colspan="2"><input type="submit" value="Next..." /></td></tr>';
    }

    function 
    WriteForm($choice)
    {
        switch(
    $choice)
        {
            case 
    1PersonalForm();
                    break;
            case 
    2ShippingForm();
                    break;
            case 
    3ProductInfoForm();
                    break;
            case 
    4ConfirmForm();
                    break;
            default: 
    PersonalForm();
        }
    }

    ?>

    <html>
    <head>
    <title></title>
    </head>
    <body>

    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

        <?php 
            WriteForm
    ($step);
        
    ?>

    </form>

    </body>
    Admittedly, I am no expert on PHP. PHP Experts, is this a terrible idea? Feedback always helps!

    _aA

  3. #3
    Hi actionAction, thanks for replying. I decided to put all of the steps into one page. The form itself ended up not being too long. I've turned it into the following steps:

    1. Fill out the form
    2. On submit, confirm all entered values
    3. Click submit again to send request.

    The problem I'm having now is that the processing is not liking spaces. For example, I declared a variable called $msg, which houses all of the information the user entered into the fields. Then I use the mail() function to send $msg, but anytime there is a space in any of the values, its stops and ignores everything thereafter. Here is the code for my entire page:
    Code:
    <?php
    $to = "jpearson@syndicateinteractive.com"; 
    $subject = "Rent-A-Shed Quote Request!";
    /* Variables for personal information */
    $headers =  $_POST["name"];
    $email = $_POST["email"];
    $phone = $_POST["phone"];
    $address1 = $_POST["address1"];
    $address2 = $_POST["address2"];
    $city = $_POST["city"];
    $zip = $_POST["zip"];
    
    /* Variables for delivery information */
    $cname = $_POST["cname"];
    $cphone = $_POST["cphone"];
    $daddress1 = $_POST["daddress1"];
    $daddress2 = $_POST["daddress2"];
    $dcity = $_POST["dcity"];
    $dzip = $_POST["dzip"];
    
    /* Variables for product information */
    $model = $_POST["model"];
    $ddate = $_POST["ddate"];
    $pdate = $_POST["pdate"];
    
    $msg = "Personal Information:\n\n$headers\n$email\n$phone\n\n$address1\n$address2\n$city $zip\n\nDelivery Information:\n\n$cname\n$cphone\n\n$daddress1\n$daddress2\n$dcity $dzip\n\nProduct Information:\n\nModel: $model\nDelivery Date: $ddate\nPickup Date: $pdate";
    ?>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Rent-A-Shed :: Portable Storage Containers</title>
    <link href="styles.css" rel="stylesheet" type="text/css" />
    <script src="Scripts/AC_RunActiveContent.js" type="text/javascript"></script>
    <script src="Scripts/validate.js"></script>
    </head>
    
    <body>
    <div id="container"><a href="index.php"><img src="images/logo.png" alt="Rent-A-Shed" width="190" height="45" border="0" class="logo" /></a><img src="images/navbar.png" width="434" height="30" border="0" usemap="#Map" class="navbar" />
    <map name="Map" id="Map"><area shape="rect" coords="1,0,79,29" href="about.php" alt="about" />
    <area shape="rect" coords="78,1,169,36" href="containers.php" alt="containers" />
    <area shape="rect" coords="168,2,247,29" href="purchase.php" alt="purchase" />
    <area shape="rect" coords="246,1,342,29" href="quote.php" alt="quote" />
    <area shape="rect" coords="341,1,442,32" href="contact.php" alt="contact us" />
    </map>
    <form action="index.php" class="mlistform"><img src="images/envelope.png" width="27" height="15" class="envelope" /><span class="signup"> Sign Up for Our Mailing List!</span> <input name="textfield" type="text" class="mlistfield" /> <input type="image" class="signupbutton" src="images/signup.jpg" /></form>
    <div id="main">
    <div id="left"><h1>Quick Quote</h1>
    <?php
    if(isset($_POST["submit"])){ 
        echo "<p>Please verify that all information is correct.</p>";
        echo "<form action=$PHP_SELF method='post'>";
        echo "<h2>Personal Information</h2>";
        echo "<p>".$name."</p>";
        echo "<p>".$email."</p>";
        echo "<p>".$phone."</p>";
        echo "<p>".$address1."</p>";
        echo "<p>".$address2."</p>";
        echo "<p>".$city." ".$zip."</p><br />";
        
        echo "<h2>Delivery Information</h2>";
        echo "<p>".$cname."</p>";
        echo "<p>".$cphone."</p>";
        echo "<p>".$daddress1."</p>";
        echo "<p>".$daddress2."</p>";
        echo "<p>".$dcity." ".$dzip."</p><br />";
        
        echo "<h2>Product Information</h2>";
        echo "<p>".stripslashes($model)."</p>";
        echo "<p>Delivery Date:"." ".$ddate."</p>";
        echo "<p>Pickup Date:"." ".$pdate."</p><br />";
        echo "<input type='hidden' name='message' value=".$msg." />";
        echo "<input name='quote' type='submit' class='submit' value='Request It!' /></form>";
    } 
    else if(isset($_POST["quote"])){
        mail($to, $subject, $message, "From: ".$headers."<".$email.">"); 
        echo "<p>We appreciate your inquiry.  One of our associates will contact you soon.</p>";
    }
    else{
    ?>
    <p>Fill out the form below to get a quote.</p><br />
    <p>*Required Fields</p><br />
    <h2>Personal Information</h2>
    <form action=<?php echo $PHP_SELF; ?> method="post" name="quoteForm" onsubmit="javascript:return validateQuote()">
    <p>*Full Name:</p>
    <input name="name" type="text" class="inputs" id="name" />
    <br /><br />
    <p>*Email:</p>
    <input name="email" type="text" class="inputs" id="email" />
    <br /><br />
    <p>*Phone:</p>
    <input name="phone" type="text" class="inputs" id="phone" />
    <br /><br />
    <p>*Address</p>
    <input name="address1" type="text" class="inputs" id="address1" />
    <input name="address2" type="text" class="address2" id="address2" />
    <br /><br />
    <p>*City: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*Zip:</p>
    <input name="city" type="text" class="city" id="city" /><input name="zip" type="text" class="zip" id="zip" />
    <br /><br /><h2>Delivery Information</h2><br />
    <p>*Contact Name:</p>
    <input name="cname" type="text" class="inputs" id="cname" />
    <br /><br />
    <p>*Contact Phone Number:</p>
    <input name="cphone" type="text" class="inputs" id="cphone" />
    <br /><br />
    <p>*Delivery Address</p>
    <input name="daddress1" type="text" class="inputs" id="daddress1" />
    <input name="daddress2" type="text" class="address2" id="daddress2" />
    <br /><br />
    <p>*City: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*Zip:</p>
    <input name="dcity" type="text" class="city" id="dcity" /><input name="dzip" type="text" class="zip" id="dzip" />
    <br /><br /><h2>Product Information</h2><br />
    <p>*Container Model: </p>
    <label>
    <select name="model" class="inputs" id="model">
      <option selected="selected">Please Select...</option>
      <option>20' Standard Door Container</option>
      <option>20' Double Door Container</option>
      <option>8.5' x 40' Container</option>
      <option>9.5' x 40' High Cube Container</option>
    </select>
    </label>
    <br />
    <br />
    <p>*Delivery Date:</p>
    <input name="ddate" type="text" class="inputs" id="ddate" />
    <br /><br />
    <p>*Pickup Date: </p>
    <input name="pdate" type="text" class="inputs" id="pdate" />
    <br /><br />
    <input name="submit" type="submit" class="submit" value="Submit" />
    </form><br /><br /><br />
    <?php } ?>
    </div>
    </div>
    <div id="footer"><p>&copy; 2007 BJ Equipment, Inc All Rights Reserved</p><img src="images/footer_tag.png" class="footertag" /></div>
    </div>
    </body>
    </html>
    If you have an answer for this one, I'd appreciate it. I've built a million forms and have never experienced this. Thanks!
    Quote Originally Posted by actionAction View Post
    Hi,
    There are practically an infinite number of ways this could be accomplished (as I am sure you are aware). You could just test to see if $_POST variables are set and rewrite the HTML based on those factors. Hidden input types might be helpful, but once the variable is set, you don't really need to passing the hidden value anymore.

    The example I give here sets all of your variables to the corresponding form names like you would expect. I also initialize a variable called "step" at 1, and based on what variables are/aren't set, change the value of step (Seems pretty logical right? If name is set and shipping address isn't, step is 2). I then wrote a function called WriteForm() with the step variable as its parameter; it performs a switch statement to decide which form to write. Each form has its own function to echo the appropriate form content e.g. PersonalForm(). I am really more of a C++ person, so I can't really tell you if this is the best way, but with enough tweeking, it should work! Hope it helps! (BTW, this is just a simplified example that probably won't work as is):
    PHP Code:
    <?php

    $name 
    $_POST['Name'];
    $address $_POST['Address'];
    $productInfo $_POST['ProductInfo'];
    #etc

    //Set the default of which step you are on here
    $step 1;

    if(!isset(
    $name))
    {
        
    $step 1;
    }
    elseif(isset(
    $name) && !isset($address))
    {
        
    $step 2;
    }
    elseif(isset(
    $address) && !isset($productInfo))
    {
        
    $step 3;
    }
    else
    {
        
    $step 4;
    }

    //Example form writer
    function PersonalForm()
    {
        echo 
    '<tr><td>Name</td><td><input type="text" name="Name" /></td></tr>';
        echo 
    #other personal stuff
        
    echo '<tr><td colspan="2"><input type="submit" value="Next..." /></td></tr>';
    }

    function 
    WriteForm($choice)
    {
        switch(
    $choice)
        {
            case 
    1PersonalForm();
                    break;
            case 
    2ShippingForm();
                    break;
            case 
    3ProductInfoForm();
                    break;
            case 
    4ConfirmForm();
                    break;
            default: 
    PersonalForm();
        }
    }

    ?>

    <html>
    <head>
    <title></title>
    </head>
    <body>

    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

        <?php 
            WriteForm
    ($step);
        
    ?>

    </form>

    </body>
    Admittedly, I am no expert on PHP. PHP Experts, is this a terrible idea? Feedback always helps!

    _aA

  4. #4
    actionAction's Avatar
    1,142
    posts
    humanBeing._beard=true;
    You could try the Heredoc syntax like this:
    Code:
    $msg = <<<EOT 
        Personal Information:\n\n$headers\n$email\n$phone\n\n$address1\n
        $address2\n$city $zip\n\nDelivery Information:\n\n$cname\n$cphone
        \n\n$daddress1\n$daddress2\n$dcity $dzip\n\nProduct Information:\n
        \nModel: $model\nDelivery Date: $ddate\nPickup Date: $pdate
    EOT;

  5. #5
    Got it. Thanks everyone. It was a concatenation problem with the inputs that I was echoing. All up and running just fine now. Thanks!

  6. #6

    Problems

    For some reason after step 1 and step 2 the script goes back to step 1... I don't get it.

    PHP Code:
    <?php

    $name 
    $_POST['name'];
    $address $_POST['address'];
    $productInfo $_POST['ProductInfo'];


    //Set the default of which step you are on here
    $step 1;

    if(!isset(
    $name))
    {
        
    $step 1;
    }
    elseif(isset(
    $name) && !isset($address))
    {
        
    $step 2;
    }
    elseif(isset(
    $address) && !isset($productInfo))
    {
        
    $step 3;
    }
    else
    {
        
    $step 4;
    }

    //Step Functions && Step Outputs
    function PersonalForm()
        {
        echo 
    '<tr><td>Name</td><td><input type="text" name="name" /></td></tr>';
        echo 
    '<tr><td colspan="2"><input type="submit" value="Next..." /></td></tr>';
        }

    function 
    ShippingForm()
        {
          echo 
    '<tr><td>Address</td><td><input type="text" name="address" /></td></tr>';
        echo 
    '<tr><td colspan="2"><input type="submit" value="Next..." /></td></tr>';
        }

    function 
    ProductInfoForm()
        {
        echo 
    'step3';
        }
        
    function 
    ConfirmForm()
        {
        echo 
    'step4';
        }

    //

    function WriteForm($choice)
    {
        switch(
    $choice)
        {
            case 
    1PersonalForm();
                    break;
            case 
    2ShippingForm();
                    break;
            case 
    3ProductInfoForm();
                    break;
            case 
    4ConfirmForm();
                    break;
            default: 
    PersonalForm();
        }
    }

    ?>

    <html>
    <head>
    <title></title>
    </head>
    <body>

    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

        <?php 
            WriteForm
    ($step);
        
    ?>

    </form>

    </body>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012