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
- <input type =' text ' class =' input-xxsmall ' name =' engine_no[] ' id =' engine_no ' value =' ' placeholder =' Engine No '>
-
<input type =" submit " value =" Submit " >
- <script>
- $(function(){
- $('input[type=submit]').on('click', function(){
- $.post("http://domain.com.ph/home_members/ajax_engine_no/",
- $('#my-form').serialize(),
- function(result){
- if($.trim(result) != ''){
- //there are errors
- alert(result);
- } else {
- //No error, submitting the form now to its action "save.php"
- $('#my-form').submit();
- }
- });
- //form will not wait for the ajax response and will be submitted, so by default stop submitting the form
- return false;
- });
- });
- </script>
Controllers:
- public function ajax_engine_no(){
-
-
- //engine number is an array of engine number posted by form
- $engineNumbers = $this->input->post("engine_no");
- //by default return message will be blank
- $msg = '';
- //creating an array of non empty engine number posted, as user can left engine number as blank
-
- $tempEngineNumbers = array();
- //looping through all engine numbers posted
- foreach($engineNumbers as $engineNumber){
- //checking engine number is blank or not and if not then pushing into tempEngineNumbers array
- if(trim($engineNumber) != ''){
- array_push($tempEngineNumbers, $engineNumber);
- }
- }
- //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
- if(count($tempEngineNumbers) === 0){
- $msg = 'No Engine Numbers Posted, Please enter Engine Numbers';
- } else {
- //checking for duplicates
-
- //imploding all tempEngineNumbers as comma seperated string so that can be used in MySQL in condition
- //insetead of running a seperate SQL queries for all engine numbers, we can check duplicate by one query
- $engNum = implode("','", $tempEngineNumbers);
- // $engNum = implode('"','"', $tempEngineNumbers);
- $engNum = "'".$engNum."'";
- //building the SQL query
- $sql = "SELECT * FROM `purchase_order_qty` WHERE `engine_no` IN ($engNum)";
-
- $qry = mysqli_query($link, $sql) or die(mysqli_error($link));
-
- //getting numbers of rows returned by this query, if more than zero then there are duplicates else all are new
- $duplicates = @mysqli_num_rows($qry);
- //if duplicates are more than 0 then creating message for user to tell which engine numbers are duplicates
-
- if($duplicates > 0){
- //looping throug the sql results to get the duplicate engine numbers
- // i prefer to use mysqli_fetch_assoc against mysqli_fetch_array
- while($r = mysqli_fetch_assoc($qry)){
- $msg .= "Engine Number ".$r['engine_no']." already exists, please select different engine number. ";
- }
- }
- }
- echo $msg;
-
- }