Ant Power
Ant Power

Reputation: 51

PHP Form validation, error message output

Can anyone with more experience than me take a look at this page and figure out why the php validation isn't working. This is the first time I've programmed in PHP, I've run it through PHP lint, there are no fatal errors in the code, just need to get the error messages outputting when the user tries to submit the form. The JS validation is working fine, the red error messages are the jQuery validation, so it might be best to turn JS off.

Thanks

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

Edit here's the code

//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>

Upvotes: 1

Views: 2522

Answers (2)

Ant Power
Ant Power

Reputation: 51

//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>

Upvotes: 0

phpisuber01
phpisuber01

Reputation: 7715

Look into the following functions:

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

These will override your error reporting configuration. Make sure to remove it when you go to production.

Upvotes: 1

Related Questions