html - Php registration error validation -
i have been practicing php , mongodb. developing simple web application using oop.
i created class called user has methods user adduser, deleteuser etc. add user, form carry out simple validation tasks, not sure how. here class add new user:
function createuser($username, $name, $email, $password){ $user = array( 'username' => $username, 'name' => $name, 'email' => $email, 'password' => $password ); if ($this->db->count(array('username' => $username)) == 0) { $this->db->insert($user); return true; } else { echo 'username taken'; } }
and html:
<?php session_start(); include_once 'user.php'; include './templates/header.php'; if (isset($_post['register']) && ($_post['register']) == ($_post["register"])) { $user = new user(); $return = $user->createuser( $_post['username'], $_post['name'], $_post['email'], $_post['password'], $_post['password2']); } if ($return == true) { echo 'you have registered'; } else { echo '</br>' . 'sorry, try again'; } ?> <div class="container"> <div class="jumbotron"> <form method="post" action=""> <label>username: </label><br> <input name="username" type="text" ><br><br> <label>name: </label><br> <input name="name" type="text"><br><br> <label>email: </label><br> <input name="email" type="email" ><br><br> <label>password: </label><br> <input name="password" type="password" ><br><br><br> <label>repeat password: </label><br> <input name="password2" type="password" ><br><br><br> <input name="register" class="btn btn-primary btn-lg" type="submit" value="register"><br> </form> </div> </div>
please feel free correct me on other mistakes may notice. know there bound some.
i wrote simple example simple , useful
class validation { public $currentvalue; public $values = array(); public $errors = array(); public function __construct() { parent::__construct(); // echo "burasi model sayfasi "; } public function post($key){ if(isset($_post[$key])){ $this->values[$key] = $_post[$key]; $this->currentvalue = $key; return $this; }else{ die("hi boo boo ! form values empty");} } public function isempty(){ if(empty($this->values[$this->currentvalue])){ $message='the form emppty'; $this->errors[$this->currentvalue]['empty'] =''.$message.''; } return $this; } public function submit(){ if(empty($this->errors)){ return true; }else{ return false; } } }
this example how can use ? firstly need yo call class
$form = new validation (); $form->post("all need write post name here ") ->isempty(); if($form->submit()){ //everyting ok ! cann add delet or update data }else{ $data["error"] = $form->errors; // , sett errorr mesages array can show user errormesages ! done } }
Comments
Post a Comment