Sunday, August 30, 2015

Basic validation in php

<?php
if($_SERVER['REQUEST_METHOD'] == "POST"){
    $errors = array();
    if(empty($_POST['name'])){
        $errors['name'] = "You have to enter name to in name field";
    }else{
        $name = $_POST['name'];
    }
    if(empty($_POST['email'])){
        $errors['email'] = "You have to enter email adddress to in email field";
    }else{
        $email = $_POST['email'];
    }
    if(empty($_POST['mobile'])){
        $errors['mobile'] = "You have to enter mobile number to in mobile field";
    }elseif(!is_numeric($_POST['mobile'])){
        $errors['mobile'] = "mobile number have to be numeric value";
    } else{
        $mobile = $_POST['mobile'];
    }
    if(empty($_POST['company'])){
        $errors['company'] = "You have to enter company to in company field";
    }else{
        $company = $_POST['company'];
    }

}

?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
    <style>
        .red{
           border: 2px solid #ff0000;
        }
        .green {
            border: 2px solid green;
        }
    </style>
</head>
<body>
<div class="container">
    <h1>Please Enter the each field of this form </h1>
    <form method="post" action="">
        <div class="form-group">
            <label for="name">Name</label>
            <div class="alert-danger"> <?php if(isset($errors['name'])){ echo $errors['name']; } ?> </div>

            <input value="<?php if(isset($name)){echo $name;} ?>" type="text" name="name" class="form-control <?php if(isset($errors['name'])){ echo 'red'; } ?> <?php if(isset($name)){echo 'green';} ?> " id="name" placeholder="Enter name">
        </div>
        <div class="form-group">
            <label for="email">Email</label>
            <div class="alert-danger"> <?php if(isset($errors['email'])){ echo $errors['email']; } ?> </div>
            <input value="<?php if(isset($email)){echo $email;} ?>" type="text" name="email" class="form-control <?php if(isset($errors['email'])){ echo 'red'; } ?> <?php if(isset($email)){echo 'green';} ?>  " id="email" placeholder="Enter email">
        </div>
        <div class="form-group">
            <label for="mobile">Mobile</label>
            <div class="alert-danger"> <?php if(isset($errors['mobile'])){ echo $errors['mobile']; } ?> </div>
            <input value="<?php if(isset($mobile)){echo $mobile;} ?>" type="text" name="mobile" class="form-control <?php if(isset($errors['mobile'])){ echo 'red'; } ?>  <?php if(isset($mobile)){echo 'green';} ?> " id="mobile" placeholder="Enter mobile">
        </div>
        <div class="form-group">
            <label for="company">Company</label>
            <div class="alert-danger"> <?php if(isset($errors['company'])){ echo $errors['company']; } ?> </div>
            <input value="<?php if(isset($company)){echo $company;} ?>" type="text" name="company" class="form-control <?php if(isset($errors['company'])){ echo 'red'; } ?> <?php if(isset($company)){echo 'green';} ?>  " id="company" placeholder="Enter company">
        </div>
        <button type="submit" name="" class="btn btn-default">Submit</button>
    </form>
</div>

</body>
</html>

No comments:

Post a Comment

css snippet for blogger code highlighting

code, .code {     display: block;     background: beige;     padding: 10px;     margin: 8px 15px; }