douguan8940 2010-04-08 21:33
浏览 81

PHP表单验证提交问题

Every time I try to submit the form and I have not entered nothing in the year field I get Incorrect year! how can I still submit the form without having to enter a year. In other words leaving the year field blank and not getting a warning?

Here is the PHP code.

if(preg_match('/^\d{4,}$/', $_POST['year'])) {
    $year = mysqli_real_escape_string($mysqli, $_POST['year']);
} else {
    $year = NULL;
}

if($year == NULL) {
    echo '<p class="error">Incorrect year!</p>';
} else {
    //do something
}
  • 写回答

3条回答 默认 最新

  • dsnnvpobaljihv3490 2010-04-08 21:41
    关注
    if(preg_match('/^\d{4,}$/', $_POST['year'])) {
        $year = mysqli_real_escape_string($mysqli, $_POST['year']);
    } else if(empty($_POST['year'])){
        $year = '';
    } else {
        $year = null;
    } 
    
    if($year == NULL) {
        echo '<p class="error">Incorrect year!</p>';
    } else {
        //do something
    }
    

    Perhaps I'm misunderstanding, but this sounds like what you want.

    评论

报告相同问题?