0

我已经成功地显示和发送了所有文本字段值,但是我在检查是否选择了日、月和年时遇到了问题,如果没有选择则显示错误消息。一旦选择并通过验证,我将使用表单脚本发送值。

这是我所做的,如果有人可以帮助我验证下拉选项,将值发送到电子邮件地址,并在通过提交后重定向到“谢谢”页面,我将不胜感激。

<form name="form1" e class="membership-form membership-form-stage-two" method="post" action="form-one.php">
    <div class="half">
        <h4>First Name</h4>
        <input name="firstname" type="text" id="firstname" value="<?php echo $_POST['firstname']; ?>" class="">
    </div>
    <div class="half lasthalf">
        <h4>Last Name</h4>
        <input name="lastname" type="text" id="lastname" value="<?php echo $_POST['lastname']; ?>" class="">
    </div>

    <h4>Date of Birth</h4>
    <select class="day" name="day">
        <option>Day</option>
        <option value="">1</option>
        <option value="">2</option>
    </select>

    <select class="month" name="month"> 
        <option>Month</option>
        <option value="">1</option>
        <option value="">2</option>
    </select>

    <select class="year" name="year">
        <option>Year</option>
        <option value="">1</option>
        <option value="">2</option>
    </select>

    <h4>Email Address</h4>
    <input name="emailaddress" type="text" id="emailaddress" value="<?php echo $_POST['emailaddress']; ?>" class="">

    <h4>Select your Favourite </h4>
    <div class="favorite">
        <select>
            <option value="North"> North</option>
            <option value="Central">Central</option>
            <option value="East">East</option>
        </select>
    </div>

    <h4>Company Name (for Business Buddies)</h4>
    <input name="companyname" type="text" id="companyname" value="<?php echo $_POST['companyname']; ?>" class="">

    <input type="submit" name="Submit" value="Finish" class="membership-finish">
</form>    

<?php
if (isset($_POST['Submit'])) {
    if ($_POST['firstname'] != "") {
        $_POST['firstname'] = filter_var($_POST['firstname'], FILTER_SANITIZE_STRING);
        if ($_POST['firstname'] == "") {
            $errors .= 'Please enter a valid name.<br/><br/>';
        }
    } else {
        $errors .= 'Please enter your name.<br/>';
    }

    if ($_POST['lastname'] != "") {
        $_POST['lastname'] = filter_var($_POST['lastname'], FILTER_SANITIZE_STRING);
        if ($_POST['lastname'] == "") {
            $errors .= 'Please enter a valid name.<br/><br/>';
        }
    } else {
        $errors .= 'Please enter your name.<br/>';
    }

    if ($_POST['emailaddress'] != "") {
        $emailaddress = filter_var($_POST['emailaddress'], FILTER_SANITIZE_EMAIL);
        if (!filter_var($emailaddress, FILTER_VALIDATE_EMAIL)) {
            $errors .= "$emailaddress <strong>NOT</strong> a valid email address.<br/><br/>";
        }
    } else {
        $errors .= 'Please enter your email address.<br/>';
    }


    if ($_POST['companyname'] != "") {
        $_POST['companyname'] = filter_var($_POST['companyname'], FILTER_SANITIZE_STRING);
        if ($_POST['companyname'] == "") {
            $errors .= 'Please enter a valid name.<br/><br/>';
        }
    } else {
        $errors .= 'Please enter your name.<br/>';
    }


    if (!$errors) {
        $mail_to = 'ad@test.com';
        $subject = 'test';
        $message  = 'Name: ' . $_POST['firstname'] .' ' . $_POST['lastname'] . "\n";
        $message .= 'Email Address: ' . $_POST['emailaddress'] . "\n";
        $message .= 'Company: ' . $_POST['companyname'] . "\n";
        mail($mail_to, $subject, $message);  

        header( 'Location: thank-you-one.php' ) ;

    } else {
        echo "<div class='error-message'><span>Please complete the fields in red</span></div>";
    }
}
?>
4

2 回答 2

1

只需在检查后添加此行companyname

if(!checkdate($_POST['month'],$_POST['day'],$_POST['year'])){
      $errors .= 'Please enter a valid DOB<br/><br/>';
} 
于 2013-07-09T16:53:50.417 回答
0

如果<select没有name="xx"属性,则该字段的值不会回传到服务器。因此,为每个添加一个名称。

您还必须在其中添加一个值,value=""因为它实际上是回传到服务器的,而不是<option>1</option>标签之间的值。

<h4>Date of Birth</h4>
<select class="day" name="day">
    <option>Day</option>
    <option value="0">Select day</option>
    <option value="1">1</option>
    <option value="2">2</option>
</select>

<select class="month" name="month">
    <option>Month</option>
    <option value="0">Select month</option>
    <option value="1">January</option>
    <option value="2">February</option>
    ....
</select>

<select class="year" name="year">
    <option>Year</option>
    <option value="0">Select year</option>
    <option value="2001">2001</option>
    <option value="2001">2002</option>
    ....
</select>

当您进行验证时,请记住现在您设置了 value="" ,除非另有说明,否则您将始终获得一个值,因为下拉菜单会自动选择列表中的第一个值。

现在我在每个列表中添加了一个值为“0”的项目。当您验证已选择一个条目时,您可以测试它

if ( $_POST['day'] > 0 ) // An item has been selected

等等所有日期字段。

于 2013-07-09T16:53:25.457 回答