1

Here this form will not only send mail to me but also a duplicate copy to the user.And what I want to achieve is:

  1. The entered data should be sent to mail in Html Table Format.
  2. If an input field is empty then that table data should not be sent to emails. I also referred this link: (how to make if decision in swiftmailer to check if field is empty then do not send `td` to mail) but I didn't get this.

Note: I don't want all the fields to be required. Here I have given only name, email, and address fields as required in HTML, and the remaining fields are can be left empty and submit the form.

But for now, I am not getting any mail if I submit this form

Here is my PHP code:

<?php 
if(isset($_POST['submit'])){
$to = "...@gmail.com"; // this is your Email address

$from = (!empty($_POST["email"])) ? '<tr><td>' . $_POST["email"] . '</td></tr>' : '';
$name = (!empty($_POST["name"])) ? '<tr><td>' . $_POST["name"] . '</td></tr>' : '';
$address = (!empty($_POST["address"])) ? '<tr><td>' . $_POST["address"] . '</td></tr>' : '';

$book_shelf = (!empty($_POST["book_shelf"])) ? '<tr><td>' . $_POST["book_shelf"] . '</td></tr>' : '';
$glass_ware = (!empty($_POST["glass_ware"])) ? '<tr><td>' . $_POST["glass_ware"] . '</td></tr>' : '';
$speakers = (!empty($_POST["speakers"])) ? '<tr><td>' . $_POST["speakers"] . '</td></tr>' : '';
$carpets = (!empty($_POST["carpets"])) ? '<tr><td>' . $_POST["carpets"] . '</td></tr>' : '';


$content = '<table>'.$name.$from.$address.$book_shelf.$glass_ware.$speakers.$carpets.'</table>';


$content2 = '<table>'.$name.$address.$book_shelf.$glass_ware.$speakers.$carpets.'</table>';


$headers = "From:" . $from;
$subject = "Form submission";

$headers2 = "From:" . $to;  
$subject2 = "Copy of your form submission";

mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $name . ", we will contact you shortly.";
// You can also use header('Location: thank_you.php'); to redirect to another page.
}
?>

function add(id) {
  var element = document.getElementById(id),
    value = +element.value || 0;
  element.value = value + 1;
}

function sub(id) {
  var element = document.getElementById(id),
    value = +element.value || 0;
  value--;
  if (value < 0) {
    value = 0;
  }
  element.value = value;
}
HTML Code:

<form action="" method="post">
  <div class="col-md-12">
    <div class="form-group">
      <input type="text" class="form-control" id="name" placeholder="Enter Name" name="name" required>
    </div>
  </div>
  <div class="col-md-12">
    <div class="form-group">
      <input type="email" class="form-control" id="email" placeholder="Enter E-Mail" name="email" required>
    </div>
  </div>
  <div class="col-md-6">
    <div class="form-group">
      <textarea class="form-control" rows="5" id="comment" placeholder="Address" name="address" required></textarea>
    </div>
  </div>
  <div class="col-md-3 col-sm-6">
    <div class="range-label">
      <label>Book Shelf</label>
    </div>
    <div class="range">
      <a onClick="sub('book-shelf')"><i class="fa fa-minus-circle" aria-hidden="true"></i></a><input type="text" id="book-shelf" name="book_shelf"><a onClick="add('book-shelf')"><i class="fa fa-plus-circle" aria-hidden="true"></i></a>
    </div>
  </div>
  <div class="col-md-3 col-sm-6">
    <!--start-->
    <div class="range-label">
      <label>Glass Ware</label>
    </div>
    <div class="range">
      <a onClick="sub('glass-ware')"><i class="fa fa-minus-circle" aria-hidden="true"></i></a><input type="text" id="glass-ware" name="glass_ware"><a onClick="add('glass-ware')"><i class="fa fa-plus-circle" aria-hidden="true"></i></a>
    </div>
  </div>
  <!--end-->

  <div class="col-md-3 col-sm-6">
    <!--start-->
    <div class="range-label">
      <label>Speakers</label>
    </div>
    <div class="range">
      <a onClick="sub('speakers')"><i class="fa fa-minus-circle" aria-hidden="true"></i></a><input type="text" id="speakers" name="speakers"><a onClick="add('speakers')"><i class="fa fa-plus-circle" aria-hidden="true"></i></a>
    </div>
  </div>
  <!--end-->


  <div class="col-md-3 col-sm-6">
    <!--start-->
    <div class="range-label">
      <label>Carpets</label>
    </div>
    <div class="range">
      <a onClick="sub('Carpets')"><i class="fa fa-minus-circle" aria-hidden="true"></i></a><input type="text" id="Carpets" name="carpets"><a onClick="add('Carpets')"><i class="fa fa-plus-circle" aria-hidden="true"></i></a>
    </div>
  </div>
  <!--end-->

  </div>
  <!--    End of row --->
  <input type="submit" name="submit" value="Submit">
</form>
Manjeshwar
  • 105
  • 3
  • 13

2 Answers2

1

You can check if the required values are set and are not empty, if they are set, then send the email.

You will need to set its headers so that it can be sent as HTML and not plain text. Also, here is a list of supported tags in an email template

//Set headers
$headers .= 'Content-type: text/html';
$headers2 .= 'Content-type: text/html';

if(
   isset($_POST["email"]) && $_POST["email"] != "" &&
   isset($_POST["name"]) && $_POST["name"] != "" &&
   isset($_POST["address"]) && $_POST["address"] != ""
  )
  {
    // If all required fields are set, then email them
    mail($to,$subject,$content,$headers);
    mail($from,$subject2,$content2,$headers2);
    echo "Mail Sent. Thank you " . $name . ", we will contact you shortly.";
  }
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Ikhlak S.
  • 8,578
  • 10
  • 57
  • 77
0

Add HTML required Attribute to your input fields as following example

function myFunction() {

  var name = document.getElementById("usrname").value;
  var city = document.getElementById("city").value;

  if (city) {
    alert(name + " " + city)

  } else {
    alert(name)

  }


}
<form onsubmit="myFunction()">
  Name: <input type="text" id="usrname" required><br> City: <input type="text" id="city"><br>
  <input type="submit">
</form>
Nisal Edu
  • 7,237
  • 4
  • 28
  • 34