php - Form validation using if array key exists -



php - Form validation using if array key exists -

here html , php code have used:

<?php require_once('include_function.php'); require_once('validation_functions.php'); $errors = array(); $message =""; if(isset($_post['submit'])){ $username = trim($_post['username']); $password = trim($_post['password']); if (array_key_exists('submit', $_post)){ } $fields_required = array("username","password"); foreach($fields_required $field){ $value = trim($_post[$field]); if(!has_presence($value)){ $errors[$field]= ucfirst($field). " cant blank"; } } } ?> <!doctype html> <html> <head> <meta charset="utf-8"> <title>single page submission</title> </head> <body> <?php echo $message;?> <?php echo form_error($errors)?> <form action="form_with_validation.php" method="post"> username<input type="text" name="username" value=""/><br/> password<input type="password" value="" name="password" /><br/> <input type="submit" name="submit" value="submit" /> </form> </body> </html>

and these functions have used

function has_presence($value){ homecoming isset($value) && $value !==""; } function form_error($errors=array()){ $output = ""; if(!empty($errors)){ $output .="<div class=\"error\">"; $output .="please prepare next errors"; $output .="<ul>"; foreach($errors $key => $field){ $output .= "<li>{$field}</li>"; } $output .="</ul>"; $output .="</div>"; } homecoming $output; }

can please guide me validate form using if array key exists error message next or below input field or other method needful

@sandeep s k can please seek 1 time below code? :

note: please include required db connection or validation files on top of script.

<?php function has_presence($value){ homecoming isset($value) && $value !==""; } function form_error($errors=array()){ $output = ""; if(!empty($errors)){ $output .="<div class=\"error\">"; $output .="please prepare next errors"; $output .="<ul>"; foreach($errors $key => $field){ $output .= "<li>{$field}</li>"; } $output .="</ul>"; $output .="</div>"; } homecoming $output; } $errors = array(); $message =""; if(isset($_post['submit'])){ $username = trim($_post['username']); $password = trim($_post['password']); if (array_key_exists('submit', $_post)){ } $fields_required = array("username","password"); foreach($fields_required $field){ $value = trim($_post[$field]); if(!has_presence($value)){ $errors[$field]= ucfirst($field). " cant blank"; } } } ?> <!doctype html> <html> <head> <meta charset="utf-8"> <title>single page submission</title> </head> <body> <?php echo $message;?> <?php if(!empty($errors)) { ?> <div class="has_errors"> <ul> <li> there errors in input. please prepare them , seek again. </li> </ul> </div> <?php } ?> <form action="index.php" method="post"> username<input type="text" name="username" value=""/> <?php if (array_key_exists('username', $errors)) { ?> <div class="has_errors"> <ul> <li> <?php print $errors['username'];?> </li> </ul> </div> <?php } ?> <br/> password<input type="password" value="" name="password" /> <?php if (array_key_exists('password', $errors)) { ?> <div class="has_errors"> <ul> <li> <?php print $errors['password'];?> </li> </ul> </div> <?php } ?> <br/> <input type="submit" name="submit" value="submit" /> </form> </body> </html>

php

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -