简体   繁体   English

PHP 表单:如果输入字段为空,则不要将表数据发送到邮件

[英]PHP Form: If a input field is empty then do not send the table data to mail

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.输入的数据应以 Html 表格格式发送至邮件。
  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.我还提到了这个链接:( 如果在 swiftmailer 中决定检查字段是否为空然后不将 `td` 发送到邮件,如何做出决定)但我没有得到这个。

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.这里我只给出了 HTML 中要求的姓名、电子邮件和地址字段,其余字段可以留空并提交表单。

But for now, I am not getting any mail if I submit this form但是现在,如果我提交此表格,我将不会收到任何邮件

Here is my PHP code:这是我的PHP代码:

<?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>

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.您需要设置它的标题,以便它可以作为 HTML 而不是纯文本发送。 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.";
  }

Add HTML required Attribute to your input fields as following example将 HTML required Attribute 添加到您的输入字段,如下例所示

 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM