1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Form Validation problem

Discussion in 'Databases' started by bghodsi, Aug 30, 2015.

  1. #1
    Hi folks:
    For some strange reasons password fails to register This is html and some php coding--no data entry into mysql table required. Thanks.
    <!DOCTYPE HTML>
    <html>
    <head>
    <style>
    .error {
    color: #FF0000;
    }
    #form label{
    float:left;
    width:150px;
    }

    </style>
    </head>
    <body>

    <?php
    // define variables and set to empty values
    $usernameErr = $passwordErr = $firstnameErr = $surnameErr = "";
    $username = $password =$firstname= $surname = "";

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["username"])) {
    $usernameErr = "userName is required";
    }else{
    $username = test_input($_POST["username"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$username)) {
    $usernameErr = "Only letters and white space allowed";
    }
    }

    if (empty($_POST["Password"])){
    $passwordErr = "password is required";
    }else {
    $password = test_input($_POST["password"]);
    // check if password is ok
    if(!preg_match("/^[a-zA-Z0-9&<{}*^#]*$/",$password)) {
    $passwordErr = "Only letters and numbers and special characters are allowed";
    }
    }
    if (empty($_POST["firstname"])) {
    $firstnameErr = "firstName is required";
    } else {
    $firstname = test_input($_POST["firstname"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$firstname)) {
    $firstnameErr = "Only letters and white space allowed";
    }
    }
    if (empty($_POST["surname"])) {
    $surnameErr = "surname is required";
    } else {
    $surname= test_input($_POST["surname"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$surname)) {
    $usernameErr = "Only letters and white space allowed";
    }
    }

    }

    function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
    }
    ?>

    <h2>PHP Form Validation Example</h2>
    <p><span class="error">* required field.</span></p>
    <div id="form">
    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
    <label type="text" name="username">User Name:</label>
    <input type="text" name="username" value="<?php echo $username;?>">
    <span class="error">* <?php echo $usernameErr;?></span>
    <br><br>
    <label type="text" name="password">Password:</label>
    <input type="text" name="password" value="<?php echo $password;?>">
    <span class="error">*<?php echo $passwordErr;?></span>
    <br><br>
    <label type="text" name="firsname">First Name:</label>
    <input type="text" name="firstname" value="<?php echo $firstname;?>">
    <span class="error">*<?php echo $firstnameErr;?></span>
    <br><br>
    <label type="text" name="surname">Surname:</label>
    <input type="text" name="surname" value="<?php echo $surname;?>">
    <span class="error">*<?php echo $surnameErr;?></span>
    <br><br>
    <input type="submit" name="submit" value="Submit">
    </form>
    </div>
    /body>
    </html>
     
    bghodsi, Aug 30, 2015 IP
  2. CristianG.

    CristianG. Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #2
    if (empty($_POST["Password"])) {
       $passwordErr = "password is required";
    } else {
       $password = test_input($_POST["password"]);
       // check if password is ok
       if(!preg_match("/^[a-zA-Z0-9&<{}*^#]*$/",$password)) {
         $passwordErr = "Only letters and numbers and special characters are allowed";
       }
    }
    Code (markup):
    if (empty($_POST["Password"])) {
    Code (markup):
    you need to the lowercase on Password
    if (empty($_POST["password"])) {
    Code (markup):

    YOUR CODE :

    
    <!DOCTYPE HTML>
    <html>
       <head>
       <style>
         .error {
           color: #FF0000;
         }
         #form label{
           float:left;
           width:150px;
         }
       </style>
       </head>
    <body>
    
    <?php
    // define variables and set to empty values
    $usernameErr = $passwordErr = $firstnameErr = $surnameErr = "";
    $username = $password =$firstname= $surname = "";
    
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
       if (empty($_POST["username"])) {
         $usernameErr = "userName is required";
       } else {
         $username = test_input($_POST["username"]);
         // check if name only contains letters and whitespace
         if (!preg_match("/^[a-zA-Z ]*$/",$username)) {
         $usernameErr = "Only letters and white space allowed";
         }
       }
       if (empty($_POST["password"])) {
         $passwordErr = "password is required";
       } else {
         $password = test_input($_POST["password"]);
         // check if password is ok
         if(!preg_match("/^[a-zA-Z0-9&<{}*^#]*$/",$password)) {
           $passwordErr = "Only letters and numbers and special characters are allowed";
         }
       }
       if (empty($_POST["firstname"])) {
         $firstnameErr = "firstName is required";
       } else {
         $firstname = test_input($_POST["firstname"]);
         // check if name only contains letters and whitespace
         if (!preg_match("/^[a-zA-Z ]*$/",$firstname)) {
           $firstnameErr = "Only letters and white space allowed";
         }
       }
       if (empty($_POST["surname"])) {
         $surnameErr = "surname is required";
       } else {
         $surname= test_input($_POST["surname"]);
         // check if name only contains letters and whitespace
         if (!preg_match("/^[a-zA-Z ]*$/",$surname)) {
           $usernameErr = "Only letters and white space allowed";
         }
       }
    
    }
    
    function test_input($data) {
       $data = trim($data);
       $data = stripslashes($data);
       $data = htmlspecialchars($data);
       return $data;
    }
    ?>
    
    <h2>PHP Form Validation Example</h2>
    <p><span class="error">* required field.</span></p>
    <div id="form">
       <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
         <label type="text" name="username">User Name:</label>
         <input type="text" name="username" value="<?php echo $username;?>">
         <span class="error">* <?php echo $usernameErr;?></span>
         <br><br>
         <label type="text" name="password">Password:</label>
         <input type="text" name="password" value="<?php echo $password;?>">
         <span class="error">*<?php echo $passwordErr;?></span>
         <br><br>
         <label type="text" name="firsname">First Name:</label>
         <input type="text" name="firstname" value="<?php echo $firstname;?>">
         <span class="error">*<?php echo $firstnameErr;?></span>
         <br><br>
         <label type="text" name="surname">Surname:</label>
         <input type="text" name="surname" value="<?php echo $surname;?>">
         <span class="error">*<?php echo $surnameErr;?></span>
         <br><br>
         <input type="submit" name="submit" value="Submit">
       </form>
    </div>
    </body>
    </html>
    Code (markup):
     
    Last edited: Aug 30, 2015
    CristianG., Aug 30, 2015 IP