Check data if already exist in MySQL when typing on the text field

Check data if already exist in MySQL when typing on the text field

I have a form where user when clicking the button, it will then check first data to be inserted in database if already exist.

Now, I want the ajax to check, if there's duplicate entry in database once the user has done typing/ or when typing in the engine no. field instead clicking the submit button. Would that be possible?


Form script

  1. <input type =' text ' class =' input-xxsmall ' name =' engine_no[] ' id =' engine_no ' value =' ' placeholder =' Engine No '>

  2. <input type =" submit " value =" Submit " >
  1. <script>
  2.     $(function(){
  3.         $('input[type=submit]').on('click', function(){
  4.             $.post("http://domain.com.ph/home_members/ajax_engine_no/",  
  5.             $('#my-form').serialize(),
  6.             function(result){
  7.                 if($.trim(result) != ''){
  8.                     //there are errors
  9.                     alert(result);
  10.                 } else {
  11.                     //No error, submitting the form now to its action "save.php"
  12.                     $('#my-form').submit();
  13.                 }
  14.             });
  15.             //form will not wait for the ajax response and will be submitted, so by default stop submitting the form
  16.             return false;
  17.         });
  18.     });
  19. </script>




Controllers:

  1.     public function ajax_engine_no(){
  2.       
  3.    
  4.         //engine number is an array of engine number posted by form
  5.         $engineNumbers = $this->input->post("engine_no");
  6.         //by default return message will be blank
  7.         $msg = '';
  8.         //creating an array of non empty engine number posted, as user can left engine number as blank
  9.        
  10.         $tempEngineNumbers = array();
  11.         //looping through all engine numbers posted
  12.         foreach($engineNumbers as $engineNumber){
  13.             //checking engine number is blank or not and if not then pushing into tempEngineNumbers array
  14.             if(trim($engineNumber) != ''){
  15.                 array_push($tempEngineNumbers, $engineNumber);
  16.             }
  17.         }
  18.         //checking size of tempEngineNumbers, if its 0 then user didn't posted any engine number so user can't save blank value and return an error message
  19.         if(count($tempEngineNumbers) === 0){
  20.             $msg = 'No Engine Numbers Posted, Please enter Engine Numbers';
  21.         } else {
  22.             //checking for duplicates
  23.           
  24.             //imploding all tempEngineNumbers as comma seperated string so that can be used in MySQL in condition
  25.             //insetead of running a seperate SQL queries for all engine numbers, we can check duplicate by one query
  26.             $engNum = implode("','", $tempEngineNumbers);
  27.             // $engNum = implode('"','"', $tempEngineNumbers);
  28.             $engNum = "'".$engNum."'";
  29.             //building the SQL query
  30.             $sql = "SELECT * FROM `purchase_order_qty` WHERE `engine_no` IN ($engNum)";
  31.           
  32.             $qry = mysqli_query($link, $sql) or die(mysqli_error($link));
  33.           
  34.             //getting numbers of rows returned by this query, if more than zero then there are duplicates else all are new
  35.             $duplicates = @mysqli_num_rows($qry);
  36.             //if duplicates are more than 0 then creating message for user to tell which engine numbers are duplicates
  37.           
  38.             if($duplicates > 0){
  39.                 //looping throug the sql results to get the duplicate engine numbers
  40.                 // i prefer to use mysqli_fetch_assoc against mysqli_fetch_array
  41.                 while($r = mysqli_fetch_assoc($qry)){
  42.                     $msg .= "Engine Number ".$r['engine_no']." already exists, please select different engine number. ";
  43.                 }
  44.             }
  45.         }
  46.         echo $msg;
  47.        
  48.     }