此处此表单不仅会向我发送邮件,还会向用户发送副本。我想要实现的目标是:
注意:我不希望所有字段都是必需的。在这里,我只提供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>
答案 0 :(得分:1)
您可以检查所需的值是否已设置且不为空(如果已设置),然后发送电子邮件。
您需要设置其标题,以便它可以作为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.";
}
答案 1 :(得分:0)
将HTML必需属性添加到输入字段,如下例
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>