Using some Loop or repeatable fucntion in AJAX and PHP to reduce repeatability

Question

I am having a code, in which textbox border color changes, if values exist in wordpress database table. Briefly, if match1 textbox value exist in table:xrd_references, column:ref_code, then the match1 textbox border color green, otherwise red. But I am having many textbox like match1, match2, match3, match4 and match5.
Now I don’t want to repeat whole code for all the match textbox, instead if any solution can be suggested. Please take a look at my code.

<?php
global $wpdb; 
if(isset($_POST["match1"])) {
  $query = $wpdb->get_var("SELECT COUNT(*) FROM xrd_references WHERE ref_code="" . $_POST["match1"] . """);
    ob_clean();
  if($query>0) {
    echo "<style>#match1{border: 3px solid green;}</style>"; 
  }else{
    echo "<style>#match1{border: 3px solid red;}</style>"; 
  }
    exit;
}
?> 

<input type="text" name="match1" id="match1"/><br>
<input type="text" name="match2" id="match2"/><br>
<input type="text" name="match3" id="match3"/><br>
<input type="text" name="match4" id="match4"/><br>
<input type="text" name="match5" id="match5"/><br>
                
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function(){
  $('#match1').on('input', function() {
    jQuery.ajax({
    data:'match1='+$(this).val(),
    type: "POST",
    success:function(data){
        $("#match1").html(data);
    },
    error:function (){}
    });
});});
</script>

0
Dheeraj Kumar 3 months 2022-03-25T00:14:49-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse