dpg78570 2013-07-09 16:26
浏览 28
已采纳

PHP $ _POST下拉

I've successfully managed to display and send all the text fields values, but I'm having problems checking that day, month and year have been selected and displaying an error message if they haven't. Once selected and passed validation, I am sending the values using the form scripts.

Here is what I've done, If anyone could help me validate the dropdown options, send the values to the email address, and redirect to the "thank you" page once passed submission I'd most appreciate it.

<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'] . "
";
        $message .= 'Email Address: ' . $_POST['emailaddress'] . "
";
        $message .= 'Company: ' . $_POST['companyname'] . "
";
        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>";
    }
}
?>
  • 写回答

2条回答 默认 最新

  • doushang9172 2013-07-09 16:53
    关注

    Just add this line, after the check for the companyname

    if(!checkdate($_POST['month'],$_POST['day'],$_POST['year'])){
          $errors .= 'Please enter a valid DOB<br/><br/>';
    } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊