繁体   English   中英

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

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

在这里,此表单不仅会向我发送邮件,还会向用户发送副本。我想要实现的是:

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

注意:我不希望所有字段都是必需的。 这里我只给出了 HTML 中要求的姓名、电子邮件和地址字段,其余字段可以留空并提交表单。

但是现在,如果我提交此表格,我将不会收到任何邮件

这是我的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>

您可以检查所需的值是否已设置且不为空,如果已设置,则发送电子邮件。

您需要设置它的标题,以便它可以作为 HTML 而不是纯文本发送。 此外, 这里是电子邮件模板中支持的标签列表

//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.";
  }

将 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