Search code examples
phpemailpostmessageidentify

PHP Mail Form, labeling the $_POST in the $message


This is the code I use right now, and it works fine.

<?php 
if(!isset($_POST['submit']))
{
    //This page should not be accessed directly. Need to submit the form.
    echo "error; you need to submit the form!";
};
    $from = "[email protected]";
    $to = "[email protected]";
    $subject = "Customer Order";
    $message = "{$_POST['name']}
{$_POST['message']}
{$_POST['item1']} -Item 1
{$_POST['item2']} -Item 2";
    $headers = "From:" . $from;
    mail($to,$subject,$message, $headers);
    header('Location: thank-you1.html');


?>

When I received the email that is sent, whether a qty was entered for 'item1' or 'item2', the email will show -Item 1 -Item 2

How do I arrange these labels, so they will only appear when that qty box has a value.


Solution

  • See if the item variables in your form have been set before adding them to the e-mail being composed.

    <?php
    if(!isset($_POST['submit']))
    {
        //This page should not be accessed directly. Need to submit the form.
        echo "error; you need to submit the form!";
    };
        $from = "[email protected]";
        $to = "[email protected]";
        $subject = "Customer Order";
        $message = $_POST['name'] .
                   $_POST['message'];
    
        if (isset($_POST['item1'])) {
            $message .= $_POST['item1'];
        }
        if (isset($_POST['item2']) {
            $message .= $_POST['item2'];
        }
        $headers = "From:" . $from;
        mail($to,$subject,$message, $headers);
        header('Location: thank-you1.html');