[英]PHP Validation to require One of Two Form Fields filled in
我正在開發一個網站,它使用 javascript 來處理許多功能和基於 PHP 的驗證碼代碼來驗證表單字段。
效果很好,但如果沒有填寫任何字段,表單將提交。
我需要填寫兩個表單字段 ['email' 或 'phone'] 之一,但都不能留空。
當驗證碼字段留空或填寫不正確時,錯誤消息可能與拋出的錯誤消息相同。
我是 PHP 代碼的新手,一生都無法弄清楚如何調用該函數。
功能代碼為:
<?php
if(isset($_POST['send'])){
$emailFrom = "clientemail.com";
$emailTo = "clientemail.com";
$subject = "Contact Form Submission";
$first_name = strip_tags($_POST['first_name']);
$last_name = strip_tags($_POST['last_name']);
$email = strip_tags($_POST['email']);
$phone = strip_tags($_POST['phone']);
$message = strip_tags(stripslashes($_POST['message']));
$body .= "First Name: ".$first_name."\n";
$body .= "Last Name: ".$last_name."\n";
$body .= "Email: ".$email."\n";
$body .= "Phone: ".$phone."\n";
$body .= "Comments: ".$message."\n";
$headers = "From: ".$emailFrom."\n";
$headers .= "Reply-To:".$email."\n";
if($_SESSION['security_code'] == $_POST['security_code']){
$success = mail($emailTo, $subject, $body, $headers);
if ($success){
echo '<p class="yay">Your e–mail has been sent.</p>';
}
} else {
echo '<p class="oops">Something went wrong. Hit the back button and try again.</p>';
}
} else {
?>
表單域:
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post" id="contact" name="contact">
<fieldset>
<label for="name"><span style="color:#bf252b;">*</span>First Name</label>
<input type="text" id="first_name" name="first_name" minlength="2"/>
<label for="name">Last Name</label>
<input type="text" id="last_name" name="last_name" minlength="2"/>
<label for="email"><span style="color:#bf252b;">*</span> Email</label>
<input type="text" id="email" name="email" />
<label for="phone"><span style="color:#bf252b;">*</span> Phone</label>
<input type="text" id="phone" name="phone" />
<label for="message">Message</label><div style="clear:both;"></div>
<textarea id="message" name="message" cols="40" rows="10" ></textarea>
<img src="../captcha.php" id="captcha" alt="captcha" style="padding:25px 0px 20px 0px;" />
<label for="security_code">Enter captcha</label>
<input type="text" id="security_code" name="security_code" autocomplete="off" class="required"/>
<button type="submit" id="send" name="send" style="margin:0px 0px 10px 12px;">Send!</button>
</fieldset>
</form>
<?php } ?>
有一個用於運行驗證碼的 .php 文檔,但我認為有一個簡單的解決方案是否正確; 現有代碼中的一些額外代碼可以解決我的問題嗎? 如果可以的話,我真的很想避免使用 javascript 和插件。
提前致謝!!
嘗試這個,
if($_SESSION['security_code'] == $_POST['security_code'] && (!empty($email) || !empty($phone))) {
代替
if ($_SESSION['security_code'] == $_POST['security_code']) {
這不是驗證表單的便捷方式,但我希望這會有所幫助。
將required
屬性添加到您需要的字段中。
Notice: Undefined variable: body in /../sendform.php on line 14
去做:
$body = "First Name: ".$first_name."\n";
$body .= "Last Name: ".$last_name."\n";
$body .= "Email: ".$email."\n";
$body .= "Phone: ".$phone."\n";
$body .= "Comments: ".$message."\n";
if(($_SESSION['security_code'] == $_POST['security_code']) AND
(!empty($email) OR !empty($phone)) ) {
換句話說:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.