how can i work similarly with database instead of using localStorage in the below code. I am aware that javascript is client side language and php is a server side. I want to store retrieve data from database instead of localstorage. Any ajax jQuery json code to help out will be helpful
<div id="timer">timer</div>
<div id="countr">count</div>
<script>
var count=localStorage.getItem("count")? localStorage.getItem("count"):0;
var timeleft = localStorage.getItem("timeleft")? localStorage.getItem("timeleft"):60;
var downloadTimer = setInterval(function(){
timeleft --;
document.getElementById("timer").innerHTML = timeleft;
if(timeleft == 0)
setInterval(timeleft=60) && setInterval(count++);
document.getElementById("countr").innerHTML = count;
localStorage.setItem("timeleft",timeleft);
localStorage.setItem("count",count);
if(count==20)
setInterval(count=0); },100);
</script>
ajax code
<script>
    $(document).ready(function){
        $.ajax({url:"savedata.php",success:function(result){
            $("#timer"),html(result);
        }});
    };
    const data= new fdata();
    data.append("count","count");
    data.append("timeleft","timeleft");
    const xhr= new XMLHttpRequest();
    xhr.open("POST","savedata.php",true);
    xhr.send(data); 
</script>
Php code (savedata.php)
      <?php
      $db = mysqli_connect("localhost","root","","vidswapp");
      $timer=$_POST['timeleft'];
      $counter=$_POST['count'];
      $mysqli="insert into table1(timer,counter)values('$timer','$counter')"; 
      ?>
 
     
    