因此,我正在做一個聯繫表單,當我們點擊提交時,它應該發送一封電子郵件給管理員,包括輸入。電子郵件功能已經正常工作,我缺少的是驗證,以檢查輸入字段是否爲空。我嘗試在輸入標籤中添加所需的屬性。它在本地主機上工作,但由於某種原因,不在服務器上。此外,它還需要彈出一條消息,確認您已提交表單。我在這裏使用了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 protected]';
$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="[email protected]">
<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'])'以查看錶單是否完全發佈。您可以執行類似的檢查以查看錶單中是否發佈了特定的數據元素,您可以檢查發佈的字符串的長度等。構建邏輯以執行檢查並相應地發送或不發送消息。 – David