【发布时间】:2017-10-10 12:04:12
【问题描述】:
所以我正在制作一个联系表格,当我们点击提交时,它应该向管理员发送一封电子邮件,包括输入内容。电子邮件功能已经在工作,我缺少的是检查输入字段是否为空的验证。我尝试在输入标签中添加 required 属性。它可以在本地主机上运行,但由于某种原因不能在服务器上运行。此外,它需要弹出一条消息,确认您已提交表单。我在这里使用了 onclick 功能。但我无法澄清它是否有效,因为它只需要在提交成功且没有空字段时弹出消息。
这是我使用的代码。已建立连接。
<!---Mail Starts-->
<?php
if (isset($_POST['submit'])) {
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: Client<' . $_POST['mail'] . '>' . "\r\n";
$headers .= 'Reply-To: ' . $_POST['mail'] . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion() . "\r\n";
$to = 'email@gmail.com';
$subject = "Send Us A Smile - ".$_POST['name'];
$message = "
<html>
<body>
<div style='font-family:arial;width:100%;padding:5px;-moz-box-sizing:border-box;-webkit-box-sizing:border-box;box-sizing:border-box;'>
<div style='margin:0 auto;max-width:500px;padding:5px;text-align:center;background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#741C57), to(#360d29));background: -moz-linear-gradient(90deg, #360d29, #741C57);color:#fff;-moz-box-sizing:border-box;-webkit-box-sizing:border-box;box-sizing:border-box;border-radius:15px;'>
</div>
<div style='text-align:center;padding:5px;color:#666;background-color:rgba(255,255,240,1);max-width:460px;margin:0 auto; border:2px solid #000;border-radius:0 0 5px 5px;'>
<h2 style='font-weight:200;line-height:.3em;'>
Send Us A Smile <br>
</h2>
<p style='text-align:left;'>
Name : " . $_POST['name'] . "<br>
Email : " . $_POST['mail'] . "<br>
Subject : " . $_POST['subject'] . "<br>
Message : " . $_POST['message'] . "
</p>
</div>
</div>
</body>
</html>
";
mail($to,$subject,$message,$headers);
}
?>
<!---Mail Ends-->
<div class="full" id="contactForm">
<div>
<form name ="contact" action="" method="POST">
<input type="hidden" name="recipient" value="info@email.com">
<input type="hidden" name="subject" value="Webmail">
<div class="eight-eight">
<label class="two-eight" for="name">Name</label>
<input class="six-eight" type="text" name="name" placeholder="Your name" required oninvalid="this.setCustomValidity('Please enter your Name here')" oninput="setCustomValidity('')"/>
</div>
<div class="eight-eight">
<label class="two-eight" for="name">Subject</label>
<select id="subject" name="subject">
<option value="">Choose a service..</option>
<option value="">Option 1..</option>
<option value="">Option 2..</option>
<option value="others">Others</option>
</select>
</div>
<div class="eight-eight">
<label class="two-eight" for="name">Message</label>
<textarea class="six-eight" placeholder="Your message" name="message" required oninvalid="this.setCustomValidity('May we know what your message is?')" oninput="setCustomValidity('')"/></textarea>
</div>
<div class="eight-eight">
<label class="two-eight" for="name">Email</label>
<input class="six-eight" type="mail" name="mail" placeholder="Your email address" required oninvalid="this.setCustomValidity('Please enter your Email Address here')" oninput="setCustomValidity('')"/>
</div>
<input type="submit" name = "submit" value="Send" onclick="hgsubmit()">
<input type="hidden" name="redirect" value="http://www.email.com">
</form>
</div>
</div>
有其他方法吗?
【问题讨论】:
-
当然有办法做到这一点,但它会涉及到你的一些编码。对于验证,您不能隐式信任客户端验证。您可能希望包含服务器端代码以检查值是否已提交。例如,您当前检查
isset($_POST['submit'])以查看表单是否已发布。您可以执行类似的检查以查看是否在表单中发布了特定的数据元素,您可以检查发布的字符串的长度等。构建您的逻辑以执行您的检查并相应地发送或不发送消息。