PHP表单验证,错误消息输出

时间:2012-10-19 15:09:49

标签: php validation

任何比我更有经验的人都可以看一下这个页面,找出为什么php验证不起作用。 这是我第一次使用PHP编程,我通过PHP lint运行它,代码中没有致命错误,只需要在用户尝试提交表单时输出错误消息。 JS验证工作正常,红色错误消息是jQuery验证,因此最好关闭JS。

由于

http://www.cambrianvacation.co.uk/vpc/register1.php

编辑这里的代码       

//form processing
//import form info
//run this code when page submits test with js turned off
 if (isset($_POST["submit"])) {
$email = $_POST['email'];
$password = $_POST['password'];
$confirm_password = $_POST['confirm_password'];
$myArray = $errors();
function validate_firstname(){
//validate here, 
if(!isset($_POST['firstname'])){
     $errors = print 'Please enter your firstname';
    }
else if(strlen($_POST['firstname']) <=2){
     $errors = print 'Your firstname must be more than 2 characters';
}else{//data is fine send to encryption

    return $_POST['firstname'];
};
};

function validate_lastname(){
//validate here
if(!isset($_POST['lastname'])){
     $errors = print 'Please enter your lastname';
    }
else if(strlen($_POST['lastname']) <=2){
     $errors = print 'Your lastname must be more than 2 characters';
}else{//data is fine send to encryption

    return $_POST['lastname'];
};
};
function validate_username(){
//validate here
if(!isset($_POST['username'])){
     $errors = print 'Please enter your username';
    }
else if(strlen($_POST['username']) <=2){
     $errors = print 'Your username must be more than 2 characters';
}else{//data is fine send to encryption

    return $_POST['username'];
};
};      



  function is_valid_email_address($email){
    $qtext = '[^\\x0d\\x22\\xSc\\x80-\\xff]';
    $dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
    $atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c' .
    '\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
    $quoted_pair = '\\x5c[\\x00-\\x7f]';
    $domain_literal = "\\x5b($dtext|$quoted_pair)*\\x5d";
    $quoted_string = "\\x22($qtext|$quoted_pair)*\\x22";
    $domain_ref = $atom;
    $sub_domain = "($domain_ref|$domain_literal)";
    $word = "($atom|$quoted_string)";
    $domain = "$sub_domain(\\x2e$word)*";
    $local_part = "$word(\\x2e$word)*";
    $addr_spec = "$local_part\\x40$domain";
    return preg_match("!^$addr_spec$!", $email)? 1 : 0;
  };
  if (is_valid_email_address($email)){
     $errors = print 'Your email is a valid email address';
  } else{$errors = print 'Please enter a valid email address';
    };


  function validate_password($password){
    if(!isset($password)){
          $errors = print 'Please provide a password';
    }else if(strlen($password) <=5){
        $errors = print 'Your password must be at least 25 charachters long';
    }   
    else{return $password;}
};

    function confirm_password($confirm_password){

        if ($password !== $confirm_password){
             $errors = print 'Please enter the same password as above';
        }else{ $password === $confirm_password;}
            return $confirm_password;
        };
    };
 ?>

<div id="bigwrap">
        <div id = "space"></div>
    <div class="wrapper">

        <div class="content">
            <div id="form_wrapper" class="form_wrapper">

                <form class="register active" class="cmxform" action= "regprocess.php"           id="signupForm" class = "ajaxsubmit" method="get">
                    <fieldset class="ui-widget ui-widget-content ui-corner-all">
                    <h3 class="font">REGISTER</h3>
                    <div class="column">
                        <div>
                            <label for="cname">Firstname</label>
 <input id="cfirstname" name="firstname" type="text" class="required"   minlength="2" />

                        </div>
                        <div>
                            <label for="cname">Username</label>
 <input id="cusername" name="username" type="text"  class="required"  minlength="5" />

                        </div>
                        <div>
                            <label for="cemail">E-Mail:</label>
        <input id="cemail" name="email" type="text"  class="required email" />

                        </div>
                    </div>
                    <div class="column">
                        <div>
                            <label for="cname">Lastname</label>
 <input id="cname" name="lastname" type="text" class="required" class="ui-state-highlight" minlength="2" />

                        </div>
                        <div>
                            <label for="password">Password:</label>
        <input id="password" name="password" type="password"  class="required"/>

                        </div>
                        <div>
                            <label for="confirm_password">Confirm password:</label>
        <input id="confirm_password" name="confirm_password"   type="password" class="required" />

                        </div>
                    </div>
                    <div class="bottom">
                        <div class="remember">

                        </div>
                        <input type="submit" value="Register" class= "submit"/> 
                        <a href="index.html" rel="login" class="linkform">You have an account already? Log in here</a>

                        <div class="clear"></div>
                    </div>
                    <ul><?php echo $errors; ?></ul>
                </fieldset>
                </form>

2 个答案:

答案 0 :(得分:1)

查看以下功能:

error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('log_errors', TRUE);
ini_set('display_startup_errors', TRUE);

这些将覆盖您的错误报告配置。确保在投入生产时将其删除。

答案 1 :(得分:0)

//form processing
//import form info
//run this code when page submits test with js turned off
if (isset($_POST["submit"])) {
   $firstname = $_POST['firstname'];
$email = $_POST['email'];
$password = $_POST['password'];
$confirm_password = $_POST['confirm_password'];
 // $errors = array();
function validate_firstname(){
//validate here, 
 global $firstname;
 if(!isset($firstname)){

    //global $errors;
    //return  $errors;
    global $error_message1;
     $error_message1 = print 'Please enter your firstname';
    }
else if(strlen($firstname) <=2){
    //return $errors;
    global $error_message2;
     $error_message2 = print 'Your firstname must be more than 2 characters';
}else{//data is fine send to encryption

    return $firstname;
};
 };

function validate_lastname(){
//validate here
if(!isset($_POST['lastname'])){
     $errors = print 'Please enter your lastname';
    }
else if(strlen($_POST['lastname']) <=2){
     $errors = print 'Your lastname must be more than 2 characters';
}else{//data is fine send to encryption

    return $_POST['lastname'];
};
 };
function validate_username(){
//validate here
if(!isset($_POST['username'])){
     $errors = print 'Please enter your username';
    }
else if(strlen($_POST['username']) <=2){
     $errors = print 'Your username must be more than 2 characters';
}else{//data is fine send to encryption

    return $_POST['username'];
};
 };      



function is_valid_email_address($email){
    $qtext = '[^\\x0d\\x22\\xSc\\x80-\\xff]';
    $dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
    $atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c' .
    '\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
    $quoted_pair = '\\x5c[\\x00-\\x7f]';
    $domain_literal = "\\x5b($dtext|$quoted_pair)*\\x5d";
    $quoted_string = "\\x22($qtext|$quoted_pair)*\\x22";
    $domain_ref = $atom;
    $sub_domain = "($domain_ref|$domain_literal)";
    $word = "($atom|$quoted_string)";
    $domain = "$sub_domain(\\x2e$word)*";
    $local_part = "$word(\\x2e$word)*";
    $addr_spec = "$local_part\\x40$domain";
    return preg_match("!^$addr_spec$!", $email)? 1 : 0;
};
if (is_valid_email_address($email)){
     $errors = print 'Your email is a valid email address';
} else{$errors = print 'Please enter a valid email address';
    };


function validate_password($password){
    if(!isset($password)){
          $errors = print 'Please provide a password';
    }else if(strlen($password) <=5){
        $errors = print 'Your password must be at least 25 charachters long';
    }   
    else{return $password;}
};

    function confirm_password($confirm_password){

        if ($password !== $confirm_password){
             $errors = print 'Please enter the same password as above';
        }else{ $password === $confirm_password;}
            return $confirm_password;
        };
    };
  ?>

 <div id="bigwrap">
        <div id = "space"></div>
    <div class="wrapper">

        <div class="content">
            <div id="form_wrapper" class="form_wrapper">

                <form class="register active" class="cmxform" action= "regprocess.php"  id="signupForm" class = "ajaxsubmit" method="post">
                    <fieldset class="ui-widget ui-widget-content ui-corner-all">
                    <h3 class="font">REGISTER</h3>
                    <div class="column">
                        <div>
                            <label for="cname">Firstname</label>
 <input id="cfirstname" name="firstname" type="text" class="required" minlength="2" />

                        </div>
                        <div>
                            <label for="cname">Username</label>
 <input id="cusername" name="username" type="text"  class="required"  minlength="5" />

                        </div>
                        <div>
                            <label for="cemail">E-Mail:</label>
        <input id="cemail" name="email" type="text"  class="required email" />

                        </div>
                    </div>
                    <div class="column">
                        <div>
                            <label for="cname">Lastname</label>
 <input id="cname" name="lastname" type="text" class="required" class="ui-state-highlight" minlength="2" />

                        </div>
                        <div>
                            <label for="password">Password:</label>
        <input id="password" name="password" type="password"  class="required"/>

                        </div>
                        <div>
                            <label for="confirm_password">Confirm password:</label>
        <input id="confirm_password" name="confirm_password"   type="password" class="required" />

                        </div>
                    </div>
                    <div class="bottom">
                        <div class="remember">

                        </div>
                        <input type="submit" value="Register" class= "submit"/> 
                        <a href="index.html" rel="login" class="linkform">You have an account already? Log in here</a>

                        <div class="clear"></div>
                    </div>
                    <ul><?php $output =  validate_firstname(); echo "$output"; ?></ul>
                </fieldset>
                </form>