0

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 = "someone@there.com";
    $to = "someone@here.com";
    $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.

2 Answers2

0

you can change $message in below format

$message = "{$_POST['name']}";
$message .= "{$_POST['message']}";
if(isset($_POST['item1']) & !empty($_POST['item1'])) {
$message .= "{$_POST['item1'] -Item 1}";
}
if(isset($_POST['item2']) & !empty($_POST['item2'])) {
$message .= "{$_POST['item2'] -Item 2}";
}
Taylor Rahul
  • 709
  • 6
  • 19
0

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 = "someone@there.com";
    $to = "someone@here.com";
    $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');
Dave
  • 5,108
  • 16
  • 30
  • 40