Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!--Includes HTML, Head, Scripts, CSS, Body and begin of Content div-->
- <?php include realpath(__DIR__)."/layout/header.php"; ?>
- <?php
- function Validate() {
- if ($_SERVER['REQUEST_METHOD'] === 'POST') {
- return IsValid("username") && IsValid("password") && IsValid("usermail");
- } else {
- return true;
- }
- }
- function IsValid($name) {
- if ($_SERVER['REQUEST_METHOD'] === 'POST') {
- switch($name){
- case 'username':
- return !isset($_POST["username"]) || strlen($_POST["username"]) > 0;
- break;
- case 'password':
- return !isset($_POST["password"]) || strlen($_POST["password"]) > 0;
- break;
- case 'usermail':
- return strlen($_POST["usermail"]) > 0 && filter_var($_POST["usermail"], FILTER_VALIDATE_EMAIL);
- break;
- }
- } else {
- return true;
- }
- }
- function GetErrorMessage($name) {
- if ($_SERVER['REQUEST_METHOD'] === 'POST') {
- switch($name){
- case 'username':
- return !isset($_POST["username"]) || strlen($_POST["username"]) == 0 ? 'Username is required' : '';
- break;
- case 'password':
- return !isset($_POST["username"]) || strlen($_POST["password"]) == 0 ? 'Password is required' : '';
- break;
- case 'usermail':
- $message = '';
- if (strlen($_POST["usermail"]) == 0) {
- $message = $message.'E-mail is required';
- }
- if(strlen($_POST["usermail"]) != 0 && !filter_var($_POST["usermail"], FILTER_VALIDATE_EMAIL)) {
- if(strlen($message) > 0) {
- $message = $message.'<br />';
- }
- $message = $message.'"'.$_POST["usermail"].'" is not a valid e-mail address';
- }
- return $message;
- break;
- }
- } else {
- return '';
- }
- }
- ?>
- <div class="row">
- <div class="col-md-4">
- <form action="./userform.php" method="POST" role="form">
- <div class="form-group <?php echo IsValid('username') ? '' : 'has-error' ?>">
- <label for="username">Username</label>
- <input type="text" id="username" name="username" class="form-control" value="<?php echo Validate() ? '' : $_POST["username"] ?>" />
- <span class="help-block"><?php echo GetErrorMessage('username') ?></span>
- </div>
- <div class="form-group <?php echo IsValid('password') ? '' : 'has-error' ?>">
- <label for="password">Password</label>
- <input type="password" id="password" name="password" class="form-control" value="<?php echo Validate() ? '' : $_POST["password"] ?>" />
- <span class="help-block"><?php echo GetErrorMessage('password') ?></span>
- </div>
- <div class="form-group <?php echo IsValid('usermail') ? '' : 'has-error' ?>">
- <label for="usermail">E-Mail</label>
- <input type="text" id="usermail" name="usermail" class="form-control" value="<?php echo Validate() ? '' : $_POST["usermail"] ?>" />
- <span class="help-block"><?php echo GetErrorMessage('usermail') ?></span>
- </div>
- <div class="form-group">
- <button type="submit" class="btn btn-default">Submit</button>
- </div>
- </form>
- </div>
- <?php
- if ($_SERVER['REQUEST_METHOD'] === 'POST' && Validate()) {
- echo '<div class="col-md-4"><h4>Data from POST</h4><p>Username: '.$_POST["username"].'</p><p>Password: '.$_POST["password"].'</p><p>E-Mail: '.$_POST["usermail"].'</p></div>';
- } else if($_SERVER['REQUEST_METHOD'] === 'POST' && !Validate()) {
- echo '<div class="col-md-4"><h4>Data from POST is not valid</h4></div>';
- }
- ?>
- </div>
- <!--Includes End of Content div, Body and HTML-->
- <?php include realpath(__DIR__)."/layout/footer.php"; ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement