gpt4 book ai didi

javascript - PHP 表格 : If a input field is empty then do not send the table data to mail

转载 作者:行者123 更新时间:2023-11-30 21:01:56 24 4
gpt4 key购买 nike

这里这个表单不仅会给我发邮件还会给用户发一份副本。而我想要实现的是:

  1. 输入的数据应以 Html 表格格式发送到邮件。
  2. 如果输入字段为空,则不应将该表数据发送到电子邮件。我还提到了这个链接:( how to make if decision in swiftmailer to check if field is empty then do not send `td` to mail ) 但我没听懂。

注意:我不希望所有字段都是必填字段。这里我只给出了 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>

最佳答案

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

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

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

关于javascript - PHP 表格 : If a input field is empty then do not send the table data to mail,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47049470/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com