我为一个我正在为朋友做的婚礼网站建立了一份RSVP表格,我在周五和周六参加的表格中有一些下拉菜单和用餐选项。现在,所有的字段都是必需的,但是我想这样做,所以只有当这个人选择“是”参加星期五和星期六时才需要用餐选项下拉。因此,只有当这个人来参加婚礼时才需要下降用餐选项。我如何在PHP中执行此操作?
这是我的表格......
<div class="form">
<div class="validation">
<p>Oops! Please correct the highlighted fields...</p>
</div>
<div class="success">
<p>Thanks for your response!</p>
</div>
<form action="javascript:;" method="post" id="rsvp_form">
<div class="row">
<p>
<label for="first_name">First Name</label>
<input type="text" name="first_name" id="first_name" />
</p>
<p>
<select name="friday" id="friday">
<option value="">Friday Dinner?</option>
<option value="Yes">Yes</option>
<option value="No">No</option>
</select>
</p>
</div>
<div class="row">
<p>
<label for="last_name">Last Name</label>
<input type="text" name="last_name" id="last_name" />
</p>
<p>
<select name="saturday" id="saturday">
<option value="">Saturday Wedding?</option>
<option value="Yes">Yes</option>
<option value="No">No</option>
</select>
</p>
</div>
<div class="row">
<p>
<label for="rsvp_email">Email</label>
<input type="text" name="rsvp_email" id="rsvp_email" />
</p>
<p>
<select name="meal_choice" id="meal_choice">
<option value="">Meal Choice...</option>
<option value="Short Ribs">Short Ribs</option>
<option value="Red Snapper Fillet">Red Snapper Fillet</option>
<option value="Stuffed Poblano Pepper">Stuffed Poblano Pepper (Vegetarian)</option>
</select>
</p>
</div>
<input type="submit" class="button" value="RSVP" />
</form>
</div>
</div>
这是我正在运行的PHP脚本......
<?php
// Extract form contents
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$rsvp_email = $_POST['rsvp_email'];
$invite_code = $_POST['invite_code'];
$friday = $_POST['friday'];
$saturday = $_POST['saturday'];
$meal_choice = $_POST['meal_choice'];
// Validate email address
function valid_email($str) {
return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
}
// Return errors if present
$errors = "";
if($first_name =='') { $errors .= "first_name,"; }
if($last_name =='') { $errors .= "last_name,"; }
if(valid_email($rsvp_email)==FALSE) { $errors .= "rsvp_email,"; }
if($friday =='') { $errors .= "friday,"; }
if($saturday =='') { $errors .= "saturday,"; }
if($meal_choice =='') { $errors .= "meal_choice,"; }
// Send email
if($errors =='') {
$headers = 'From: <no-reply@website.com>'. "\r\n" .
'Reply-To: '.$rsvp_email.'' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$email_subject = "RSVP Form: $first_name $last_name";
$message="First Name: $first_name \n\nLast Name: $last_name \n\nRSVP Email: $rsvp_email \n\nAttending Friday: $friday \n\nAttending Saturday: $saturday \n\nMeal Choice: $meal_choice";
mail($to, $email_subject, $message, $headers);
echo "true";
} else {
echo $errors;
}
?>
答案 0 :(得分:0)
检查是否给出了$friday
或$saturday
然后检查用餐。如果两者都未输入,则不会检查用餐
if($first_name =='') { $errors .= "first_name,"; }
if($last_name =='') { $errors .= "last_name,"; }
if(valid_email($rsvp_email)==FALSE) { $errors .= "rsvp_email,"; }
if($friday =='') { $errors .= "friday,"; }
if($saturday =='') { $errors .= "saturday,"; }
if( $friday != '' || $saturday != '') {
if($meal_choice =='') { $errors .= "meal_choice,"; }
}