PHP validation checkbox

前端 未结 3 1201
野性不改
野性不改 2021-01-29 03:06

I\'ve scoured the entire internet trying to find a solution to what I\'m missing here (or doing wrong). My form doesn\'t validate even when the check box is checked. Everything

3条回答
  •  北荒
    北荒 (楼主)
    2021-01-29 03:23

    The problem with your current validation is that you're using if-else wrongly and that you're using exit on every statement which will terminate your script prematurely.

    You can do this instead:

    For Terms:
        
    
    //set an error counter to trigger the `exit`
    $error_counter = 0;
    if(trim($name) == '') {
        echo '
    Attention! You must enter your name
    '; $error_counter++; } if(trim($email) == '') { echo '
    Attention! Please enter a valid email address.
    '; $error_counter++; } if(!isEmail($email)) { echo '
    Attention! You have entered an invalid e- mail address, try again.
    '; $error_counter++; } if(trim($subject) == '') { echo '
    Attention! Please enter a subject.
    '; $error_counter++; } if(trim($comments) == '') { echo '
    Attention! Please enter your message.
    '; $error_counter++; } if(empty($terms)) { echo '
    Attention! Please agree to our terms of service.
    '; $error_counter++; } //if `$error_counter > 0 > 0` it will trigger the `exit()` to stop the script and display the errors. if($error_counter > 0){ exit(); }

    There's a more beautiful approach to do this. Feel free to add suggestions for the OP.

提交回复
热议问题