RAHULINDIA122

Validate form with AJAX, prevent form submitting on conditio

Mar 27th, 2017
72
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.      $regexp = "/^[a-z0-9_-]+(\.[a-z0-9_-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/";
  190.      if(!preg_match($regexp,$email)){
  191.      return "invalid_email";
  192.      
  193.      }
  194.      //select id from user_info WHERE u_email = '[email protected] ' LIMIT 1";
  195.      //check email already exists or not
  196.      $sql = "select id from ".$table." WHERE email = '$email' LIMIT 1";
  197.      $query = mysqli_query($this->con,$sql) or die (mysqli_error($this->con));
  198.      $count = mysqli_num_rows($query);
  199.      if($count == 0){
  200.      
  201.      return "ok";
  202.      
  203.      }else{
  204.      return "already_exists";
  205.      }
  206.      
  207.     }
  208.     public function verify_name($name){
  209.      $regexp = "/^[A-Za-z_]{2,5}$/";
  210.      //if(!preg_match($regexp,$name)){
  211.      //return "invalid_name";
  212.      return (!preg_match($regexp,$name)) ? "invalid_name" : 'good';
  213.      
  214.      
  215.     }
  216.  
  217. }
  218.  
  219. $obj = new DataOperation;
  220.    
  221.  
  222.  if(isset($_POST["check_email"])){
  223.      $email=$_POST["email"];
  224.      echo $data=$obj->verify_email("user",$email);
  225.      exit();
  226.  }
  227.    
  228.    if(isset($_POST["check_name"])){
  229.      $name=$_POST["name"];
  230.      echo $data=$obj->verify_name($name);
  231.      exit();
  232.  }
  233.  
  234. if(isset($_POST["user_name"])){
  235. $name= $_POST["user_name"];
  236.  
  237. $data=$obj->verify_email("user",$_POST["email"]);
  238.          if($data == "already_exists"){
  239.              echo "Email Already Exists Plz try other";
  240.              exit();
  241.             }else{
  242.             echo $email= $_POST["email"];
  243.         }
  244.  
  245. $user_info = array(
  246.                 "name_user" => $name,
  247.                
  248.                 "email" => $email,
  249.                
  250.                 );
  251.                 if ($obj->insert_record("user",$myarray)) {
  252.                 //header("location:../index.php?msg=Successfully inserted");
  253.                 echo "success";
  254.                 exit();
  255.                 }
  256.  
  257.  
  258. }
Add Comment
Please, Sign In to add comment