Search code examples
phpformspostpreview

PHP Review page for form data before processing them (e.g. email them)


I have HTML Form that sends only the filled forms to an email through PHP. Well I need to first send that info to a review page for customer to check with all the filled-in info, have them review and submit it again, and only then send that info to an email. How to do this?

Here is the code:

<?php
// Please specify your Mail Server - Example: mail.yourdomain.com.
ini_set("SMTP", "mail.amaderica.com");
// Please specify an SMTP Number 25 and 8889 are valid SMTP Ports.
ini_set("smtp_port", "25");
// Please specify the return address to use
ini_set('sendmail_from', 'ravila@art.com');

$name = $_POST['Attention_To'];

// Set parameters of the email
$to = "ravila@art.com";
$subject = "Art Promo Items Ordered";
$from = " nostrowski@art.com";
$headers = "From: $from";

$message =
    "Order has been placed. Attn to: $name .\n" .
    "Items:\n";
foreach ($_POST as $fieldName => $fieldValue)
{
    if (!empty($fieldValue))
        $message .= "   $fieldName: $fieldValue\n";
}

// Mail function that sends the email.
mail($to, $subject, $message, $headers);

header('Location: thank-you.html');
?>

Some of the fields in my form are silver_name_badges, coffee_mug, plastic_bag, paper_bag, candy, moist_towlette, notepad_and_pen, tuck_box, red_tie, cap, red_lanyard, etc.


Solution

  • Review page

    Make the form submit to a review page, instead of your send page (=your code from the question). Apart from rendering the review page itself (with all the data), put copy of the data to hidden form fields. Add Email submit button, which submits the data (effectively in the same format as the original form) to the send page.

    Example:

    <dl>
    <?
    if (!empty($_POST['plastic_bag']))
    {
    ?>
        <dt>Plastic bag:</dt>
        <dd><?=htmlspecialchars($_POST['plastic_bag'])?></dd>
    <?
    }
    if (!empty($_POST['paper_bag']))
    {
    ?>
        <dt>Paper bag:</dt>
        <dd><?=htmlspecialchars($_POST['paper_bag'])?></dd>
    <?
    }
    // and so forth for all fields 
    ?>
    </dl>
    
    <form action="your_mailing_script_from_your_question.php" method="post">
    <?
    foreach ($_POST as $key => $value)
    {
        echo "<input type=\"hidden\" name=\"".htmlspecialchars($key).
             "\" value=\"".htmlspecialchars($value)."\"/>\n";
    }
    ?>
    <input type="submit" value="Email this"/>
    </form>
    

    "Back" button

    In HTML4, you cannot have two buttons on the same form submit the form to a different URL. So there are two options:

    • Easier: Make two forms, each including all the hidden fields, and one button. Each form submits to a different URL, one to email and one back to the form.
    • Keep one form that submits to, let's say the email URL, but the script there checks what button was pressed (you have to name the button and check empty($_POST["button_name"])). then it detects that "Back" button was pressed, it redirects the post back to the form URL.

    In HTML5, you can have every button to submit to different URL. Check formaction attribute of input tag. I do not know, if you can afford to use HTML5. Check support for the attribute in browsers.

    Of course, you have to modify the form script to fill in the form with data, submitted by "Back" button. E.g.:

    <p>
    <label for="plastic_bag">Plastic bag:</label>
    <?
    $value =
        !empty($_POST["plastic_bag"]) ? htmlspecialchars($_POST["plastic_bag"]) : NULL;
    ?>
    <input name="plastic_bag" id="plastic_bag" value="<?=$value?>"/>
    </p>