RAHULINDIA122

Validate form with AJAX, prevent form submitting on conditio

Mar 27th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.86 KB | None | 0 0
  1. index.php
  2. ------------------------------------------------------------------------------------------------------------------
  3. <form onsubmit="return false"  class="form-horizontal" name="ads_post" id="post_form"  method="post">
  4. <div class="form-group">
  5.       <label class="control-label col-sm-2" >Name</label>
  6.       <div class="col-sm-10">
  7.        <input type="text" class="form-control" id="name"  placeholder="Enter Name" name="user_name">
  8.        
  9.       </div>
  10.       <span class="name_error"></span>
  11.     </div>
  12.    
  13.    
  14.     <div class="form-group">
  15.       <label class="control-label col-sm-2" >email</label>
  16.       <div class="col-sm-10">
  17.         <input type="email" class="form-control" id="email"  placeholder="Enter email" name="email">
  18.       </div>
  19.       <span class="mail_error"></span>
  20.     </div>
  21.  
  22. </form>
  23. ----------------------------------------------------------------------------------------------------------------------------
  24. validate.js
  25. ----------------------------------------------------------------------------------------------------------------------------
  26. $(document).ready(function(){
  27.   function verify_name(name){
  28.     $(".name_error").hide();
  29.     if(name == ""){
  30.     $(".name_error").show();
  31.     $(".name_error").html("plz provide your name");
  32.    
  33.     }
  34.    
  35.    else{   
  36.    
  37.     $(".name_error").show();
  38.     $(".name_error").html("validating......");
  39.     $.ajax({
  40.     url:"classes/process.php",
  41.     method:"post",
  42.     data:{check_name:1,name:name},
  43.     success:function(data){
  44.  
  45.     $(".name_error").show();
  46.     if(data == "invalid_name"){
  47.     $(".name_error").html("Plz provide valid name");
  48.  
  49.  
  50.     }
  51.     else if(data == "good"){$(".name_error").html("ok");
  52.    
  53.     }
  54.     },error: function(req, status, error) {
  55.           // alert('Error: ' + req.status);
  56.             console.log(error);
  57.             console.log();
  58.             }
  59.    
  60.     })
  61.    }
  62.    
  63. }
  64.  
  65.    function verify_email(email){
  66.    
  67.     $(".mail_error").hide();
  68.     if(email == ""){
  69.     $(".mail_error").show();
  70.     $(".mail_error").html("plz provide your Email");
  71.    
  72.     }
  73.     else{  
  74.     $(".mail_error").show();
  75.     $(".mail_error").html("validating......");
  76.    
  77.     $.ajax({
  78.     url:"classes/process.php",
  79.     method:"post",
  80.     data:{check_email:1,email:email},
  81.     success:function(data){
  82.  
  83.     $(".mail_error").show();
  84.     if(data == "already_exists"){
  85.     $(".mail_error").html("Email Already Exists Plz try another");
  86.     }else if(data == "invalid_email"){
  87.  
  88.     $(".mail_error").html("Email Provided is Invalid");
  89.    
  90.     }
  91.    else if(data == "ok"){
  92.     $(".mail_error").html("ok");
  93.    
  94.     }
  95.     },error: function(req, status, error) {
  96.           // alert('Error: ' + req.status);
  97.             console.log(error);
  98.             console.log();
  99.             }
  100.    
  101.     })
  102.    }
  103.    
  104.    
  105.    }
  106.  
  107. $("#name").focusout(function(){
  108.     var name=$("#name").val();
  109.     verify_name(name);
  110.  
  111.     })
  112.      
  113.     $("#email").focusout(function(){
  114.     var email=$("#email").val();
  115.     verify_email(email);
  116.  
  117.       })
  118.  
  119.  
  120.  
  121. $("#post_form").on("submit",function(){
  122.  
  123.     var name = $("#name").val();
  124.     var email = $("#email").val();
  125.  
  126.     if(name == ""){
  127.      $(".name_error").show();
  128.      $(".name_error").html("plz provide your name");
  129.    
  130.     }
  131.    
  132.     else if(email == ""){
  133.     $(".mail_error").show();
  134.     $(".mail_error").html("plz provide your email");
  135.    
  136.     }
  137.    
  138.     else{
  139.      
  140.     $.ajax({
  141.           url:"classes/process.php",
  142.           method:"post",
  143.           data:$("#post_form").serialize(),
  144.           success:function(data){
  145.           //if(data=="ok"){
  146.           //window.location.href="index.php";
  147.           alert(data);
  148.           //}
  149.           },error: function(req, status, error) {
  150.           // alert('Error: ' + req.status);
  151.             console.log(error);
  152.             console.log();
  153.             }
  154.          
  155.          })
  156.    
  157.    
  158.     }
  159.       })         
  160. })
  161.  
  162. ----------------------------------------------------------------------------------------------------------------
  163. process.php
  164. ----------------------------------------------------------------------------------------------------------------
  165. <?php
  166.  
  167. include "dbcon.php";
  168.  
  169. class DataOperation extends database{
  170.  
  171. public $num_rec_per_page=1;
  172.  
  173. public function insert_record($table,$fields){
  174.    
  175.     $sql="";
  176.     $sql.="insert into ".$table ;
  177.     $sql.=" (".implode(",",array_keys($fields)).") values ";
  178.     $sql.="(' ".implode(" ',' ",array_values($fields))." ' )";
  179.     $query=mysqli_query($this->con,$sql);
  180.     $id = mysqli_insert_id($this->con);
  181.    
  182.         if ($query){
  183.                   return $id;
  184.               }
  185.        
  186.     }
  187.    
  188. public function verify_email($table,$email){
  189.     //ravirahul@gmail.com
  190.      $regexp = "/^[a-z0-9_-]+(\.[a-z0-9_-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/";
  191.      if(!preg_match($regexp,$email)){
  192.      return "invalid_email";
  193.      
  194.      }
  195.      //select id from user_info WHERE u_email = 'ravirhul1987@gmail.com ' LIMIT 1";
  196.      //check email already exists or not
  197.      $sql = "select id from ".$table." WHERE email = '$email' LIMIT 1";
  198.      $query = mysqli_query($this->con,$sql) or die (mysqli_error($this->con));
  199.      $count = mysqli_num_rows($query);
  200.      if($count == 0){
  201.      
  202.      return "ok";
  203.      
  204.      }else{
  205.      return "already_exists";
  206.      }
  207.      
  208.     }
  209.     public function verify_name($name){
  210.     //ravirahul@gmail.com
  211.      $regexp = "/^[A-Za-z_]{2,5}$/";
  212.      //if(!preg_match($regexp,$name)){
  213.      //return "invalid_name";
  214.      return (!preg_match($regexp,$name)) ? "invalid_name" : 'good';
  215.      
  216.      
  217.     }
  218.  
  219. }
  220.  
  221. $obj = new DataOperation;
  222.    
  223.  
  224.  if(isset($_POST["check_email"])){
  225.      $email=$_POST["email"];
  226.      echo $data=$obj->verify_email("user",$email);
  227.      exit();
  228.  }
  229.    
  230.    if(isset($_POST["check_name"])){
  231.      $name=$_POST["name"];
  232.      echo $data=$obj->verify_name($name);
  233.      exit();
  234.  }
  235.  
  236. if(isset($_POST["user_name"])){
  237. $name= $_POST["user_name"];
  238.  
  239. $data=$obj->verify_email("user",$_POST["email"]);
  240.          if($data == "already_exists"){
  241.              echo "Email Already Exists Plz try other";
  242.              exit();
  243.             }else{
  244.             echo $email= $_POST["email"];
  245.         }
  246.  
  247. $user_info = array(
  248.                 "name_user" => $name,
  249.                
  250.                 "email" => $email,
  251.                
  252.                 );
  253.                 if ($obj->insert_record("user",$myarray)) {
  254.                 //header("location:../index.php?msg=Successfully inserted");
  255.                 echo "success";
  256.                 exit();
  257.                 }
  258.  
  259.  
  260. }
Add Comment
Please, Sign In to add comment