Membuat Validasi Pendaftaran Menggunakakan Php PDO

//lh4.googleusercontent.com/-eAXyxy2JJKA/AAAAAAAAAAI/AAAAAAAAGS4/OHgzqHzCvtQ/s512-c/photo.jpg
September 23, 2018




Selamat sore sobat blogger kali ini kami akan share bagaimana membuat validasi pendaftaran menggunakan Php PDO, Kalian harus tahu pada artikel sebelumnya kami telah membahas hal yang sama tetapi menggunakan Php MySQLi.



Read More:
Membuat Validasi Verifikasi Pendaftaran Menggunakakan Php MySQLi

Kalian harus tujuan dari validasi pendaftaran di atas, tujuan utamanya adalah untuk mencegah adanya duplikat user khusunya email dalam sebuha sistem.

langsung saja pertama yang harus anda lakukan adalah membua database, buat databse dengan nama check kemudian masukkan tabel di bawah ini.

 SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";


CREATE TABLE `tblusers` (
  `id` int(11) NOT NULL,
  `FullName` varchar(120) DEFAULT NULL,
  `EmailId` varchar(100) DEFAULT NULL,
  `Password` varchar(100) DEFAULT NULL,
  `ContactNo` char(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;



INSERT INTO `tblusers` (`id`, `FullName`, `EmailId`, `Password`, `ContactNo`) VALUES
(1, 'arman', 'arman@gmail.com', '202cb962ac59075b964b07152d234b70', '123');

ALTER TABLE `tblusers`
  ADD PRIMARY KEY (`id`);


ALTER TABLE `tblusers`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;
COMMIT;
 

Kedua buat file dengan nama config.php fungsi file ini adalah sebagai koneksi kedatabse/

 <?php 
// DB credentials.
define('DB_HOST','localhost');
define('DB_USER','root');
define('DB_PASS','');
define('DB_NAME','check');
// Establish database connection.
try
{
$dbh = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME,DB_USER, DB_PASS,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
}
catch (PDOException $e)
{
exit("Error: " . $e->getMessage());
}
?> 

Ketiga buat file dengan nama index.php file ini berfungsi sebagai form register.

 <?php
session_start();
include('config.php');
error_reporting(0);
if(isset($_POST['signup']))
{
  $fname=$_POST['fullname'];
  $email=$_POST['emailid']; 
  $mobile=$_POST['mobileno'];
  $password=md5($_POST['password']); 
  $sql="INSERT INTO  tblusers(FullName,EmailId,ContactNo,Password) VALUES(:fname,:email,:mobile,:password)";
  $query = $dbh->prepare($sql);
  $query->bindParam(':fname',$fname,PDO::PARAM_STR);
  $query->bindParam(':email',$email,PDO::PARAM_STR);
  $query->bindParam(':mobile',$mobile,PDO::PARAM_STR);
  $query->bindParam(':password',$password,PDO::PARAM_STR);
  $query->execute();
  $lastInsertId = $dbh->lastInsertId();
  if($lastInsertId)
  {
    echo "<script>alert('Registration successfull. Now you can login');</script>";
  }
  else 
  {
    echo "<script>alert('Something went wrong. Please try again');</script>";
  }
}

?>
<!DOCTYPE html>
<html>
<head>
  <link rel="shortcut icon" href="images/profile.gif">
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <!-- Main CSS-->
  <link rel="stylesheet" type="text/css" href="login/css/main.css">
  <!-- Font-icon css-->
  <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
  <title>Check Availability User</title>
  <script>
    function checkAvailability() {
      $("#loaderIcon").show();
      jQuery.ajax({
        url: "check_availability.php",
        data:'emailid='+$("#emailid").val(),
        type: "POST",
        success:function(data){
          $("#user-availability-status").html(data);
          $("#loaderIcon").hide();
        },
        error:function (){}
      });
    }
  </script>
  <script type="text/javascript">
    function valid()
    {
      if(document.signup.password.value!= document.signup.confirmpassword.value)
      {
        alert("Password and Confirm Password Field do not match  !!");
        document.signup.confirmpassword.focus();
        return false;
      }
      return true;
    }
  </script>
</head>
<body>
  <section class="material-half-bg">
    <div class="cover">
    </div>
    
  </section>
  <section class="login-content">
    <div class="login-box">


     <form  class="login-form" method="post" name="signup" onSubmit="return valid();">
      <a class="brand" href="../index.html">
        <div class="thumbnail"><center><img src="images/profile.gif" height="100"/></center></div></a><p/>
        <div class="form-group">
          <input type="text" class="form-control" name="fullname" placeholder="Full Name" required="">
        </div>
        <div class="form-group">
          <input type="text" class="form-control" name="mobileno" placeholder="Mobile Number" required="">
        </div>
        <div class="form-group">
          <input type="email" class="form-control" name="emailid" id="emailid" onBlur="checkAvailability()" placeholder="Email Address" required="">
          <span id="user-availability-status" style="font-size:12px;"></span> 
        </div>
        <div class="form-group">
          <input type="password" class="form-control" name="password" placeholder="Password" required="">
        </div>
        <div class="form-group">
          <input type="password" class="form-control" name="confirmpassword" placeholder="Confirm Password" required="">
        </div>
        <div class="form-group btn-container">
          <button type="submit" name="signup" id="submit" class="btn btn-primary btn-block"><i class="fa fa-sign-in fa-lg fa-fw"></i>SIGN IN</button>
        </div>
      </form>
    </div>
  </section>
  <!-- Essential javascripts for application to work-->
  <script src="login/js/jquery-3.2.1.min.js"></script>
  <script src="login/js/popper.min.js"></script>
  <script src="login/js/bootstrap.min.js"></script>
  <script src="login/js/main.js"></script>
  <!-- The javascript plugin to display page loading on top-->
  <script src="login/js/plugins/pace.min.js"></script>
</body>
</html> 

Terakhir buat file dengan nama check_availability.php file ini berfungsi mengecek email dari tabel users.

 <?php 
require_once("config.php");
// code user email availablity
if(!empty($_POST["emailid"])) {
 $email= $_POST["emailid"];
 if (filter_var($email, FILTER_VALIDATE_EMAIL)===false) {

  echo "error : You did not enter a valid email.";
 }
 else {
  $sql ="SELECT EmailId FROM tblusers WHERE EmailId=:email";
  $query= $dbh -> prepare($sql);
  $query-> bindParam(':email', $email, PDO::PARAM_STR);
  $query-> execute();
  $results = $query -> fetchAll(PDO::FETCH_OBJ);
  $cnt=1;
  if($query -> rowCount() > 0)
  {
   echo "<span style='color:yellow'> Email already exists .</span>";
   echo "<script>$('#submit').prop('disabled',true);</script>";
  } else{
   
   echo "<span style='color:green'> Email available for Registration .</span>";
   echo "<script>$('#submit').prop('disabled',false);</script>";
  }
 }
}


?>
 

Kalian bisa melihat demonya dan mendownloadnya untuk mencoba dan melihat bagaimana cara penggunaannya secara langsung.

DEMO       DOWNLOAD

Sekian share kali ini semoga apa yang kami share bermanfaat bagi anda semua, jangan lupa tinggalkan komentar dibawah dan mulai berdiskusi dengan kami.


Related Post

SUBSCRIBE TO OUR NEWSLETTER

closed