0

我为我正在为朋友做的婚礼网站建立了一个 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;
}

?>
4

1 回答 1

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,"; }
}
于 2013-09-23T16:27:32.657 回答