I have tried to get the ID of the dynamically generated button in the following way. I tried to get the particular button ID and use it for beforeSend:function(id). But it isn't successful.
<?php
session_start();
require_once "../auth/dbconnection.php";
$output ='';
$sql= "SELECT * FROM appointment AS a INNER JOIN prescription as p ON a.apt_id=p.apt_id INNER JOIN users as u ON u.user_id=a.user_id AND a.p_id='".$_POST["id"]."'";
$result=mysqli_query($conn,$sql);
if(mysqli_num_rows($result)>0){
$output .='
<table class="table table-striped custom-table mb-0">
   <thead>
      <tr>
         <th>Appointment ID</th>
         <th>Prescription ID</th>
         <th>Doctor Name</th>
         <th>Specilization</th>
         <th>Appointment Date </th>                             
         <th>Action</th> 
         <th>Status</th> 
      </tr>
    </thead>';                                 
     while($row=mysqli_fetch_array($result)){
     $output .='
      <tr>
          <td><a href="#">'.$row["apt_id"].'</a></td>
          <td id="pres"><a href="#">'.$row["pres_id"].'</a></td>
          <td>'.$row["username"].'</td>
          <td><span class="custom-badge status-blue">'.$row["specilization"].'</span></td>
          <td>'.$row["apt_date"].'</td>
          <td>                             
          <button id="check'.$row["pres_id"].'" class="btn btn-danger custom-badge status-grey" value="Submit"  data-id='.$row['pres_id'].' onclick="alert(this.id)">   Submit  </button> </td>    
          <td> <span id="status" name="status"></span> </td>                                 
      </tr>';
}                                                 
}else{
    $output .=  '
<tr>
<td colspan="5" align="center"> No more prescriptions available :-( </td>
</tr>';
}
echo $output;   
?>
Here is my jquery
$(document).on("click","button",function(){
    
   var id = $(this).attr("data-id");
    $.ajax({
     url:"delete_record.php",
     method:"POST",
     data:{id:id},
     beforeSend:function(id)
{
    $("#check"+id).val("Checking......");
  
},   
     success:function(data){
      $('#alert_message').html('<div class="alert alert-success">'+data+'</div>');
      location.reload();
     }
    });
    setInterval(function(){
     $('#alert_message').html('');
    }, 5000); 
  });
 
     
     
    