0

I need your help. I have an auction scenario. I wrote two functions in javascript code. In this way, two counts are made according to the beginning and end of the auction. I want it to be "arac_durum" -> "Aktif" in my database when the first count is completed and "arac_durum" -> "Pasif" when the second count is completed. How can I do it, can you help?

<script>

function createCountDown(elementId, date, dateiki) {

    var countDownDate = new Date(date).getTime();

var x = setInterval(function() {


    var now = new Date().getTime();
    

    var distance = countDownDate - now;

    

    var days = Math.floor(distance / (1000 * 60 * 60 * 24));
    var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
    var seconds = Math.floor((distance % (1000 * 60)) / 1000);
    

    document.getElementById(elementId).innerHTML = "Süre ►" + days + ": " + hours + ": " + minutes + ": " + seconds;
    

    if (distance < 0) {
        clearInterval(x);

        document.getElementById(elementId).innerHTML = "Started !";
        // Here I want the "arac_durum" item to be "Aktif".
        

        
        
        var bitistarihi = new Date(dateiki).getTime();
        var y = setInterval(function() {
            var simdi = new Date().getTime();
            var distance = bitistarihi - simdi;
            var days = Math.floor(distance / (1000 * 60 * 60 * 24));
            var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
            var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
            var seconds = Math.floor((distance % (1000 * 60)) / 1000);
            document.getElementById(elementId).innerHTML = "Süre ►" + days + ": " + hours + ": " + minutes + ": " + seconds;
            if (distance < 0) {
                clearInterval(y);

                document.getElementById(elementId).innerHTML = "Finish !";
                // Here I want the "arac_durum" item to be "Pasif".
                
                }
        }, 1000);
                
        
    }
}, 1000);

}

createCountDown("<?=$araccek['arac_id']?>", "<?=$araccek['arac_gerisayim']?>", "<?=$araccek['arac_tarih']?>")
  



</script> 
 <p id="<?=$araccek['arac_id']?>"></p>

enter image description here

enter image description here

4
  • What's your specific problem with achieving your aim? If you want to send the new value to your database then first you need to send it to your server via a HTTP request, so that some PHP code can be executed to save the value into the database. A browser can trigger a HTTP request in a few ways - by the user clicking a link, by Javascript navigating the browser to a new URL, by the user submitting a form, by javascript submitting a form, or by making an AJAX request using Javascript. I would guess the last option is probably what you'll want. Commented May 5, 2020 at 22:36
  • Make a request using XMLHttpRequest / fetch for a suitable PHP script, and have the script update the DB row. Commented May 5, 2020 at 22:36
  • @Chris G Can you give me a sample update code? Commented May 5, 2020 at 22:44
  • fetch('update.php?value=123') will run update.php on the server, passing $_GET['value'] = 123 Commented May 5, 2020 at 22:48

2 Answers 2

1

First: You need a server side code to receive the flag,if flag is 1 update active, otherwise update pasif, let's crwate PHP file and name it - modify.php:

<?php
      $flag = $_POST['flag'];
      $id = 1; //You have to receive the value of the id from the client. I mean to passed with the ajax request.
      $dsn = "mysql:host=localhost;dbname=yourdatabase";
      $pdo = new PDO($dsn, 'dbuser', 'userpassword');
       if( $flag == 1)
                $value = "active";
        else
                 $value = "passif";

         $stm = $pdo->prepare("Update yourTableName set arac_durum=? WHERE keyfield = ?");
         $stm->bindValue(1, $value);
         $stm->bindValue(2, $id);
    if( $stm->execute())
       echo "updated successfuly";
     else
        echo "error";

?>

Second: Use Jquery to write AJAX code in your javascript section to send the status from the client to the server.

You are going to replace this comment by the ajax code // Here I want the "arac_durum" item to be "Pasif".

Let's do it:

$.ajax({
           Type:'POST',
            url:'modify.php',
            data:{flag:2},
             success:function(response)
             {
                 // do sth here like alert a msg
                   alert(response);
             }
      });

Above solution is enough to fix your problem, but if you are not familiar with jquery you can use (fetch) appi to send data from the client to the server.

 fetch('modify.php?flag=2')
         .then(reponse=>response.text())
         .then(data=>alert(data));

In modify.php use $_GET['flag'] instead of $_POST['flag'] because i added the parameter in the url of the page. The rest of code will remain as it is.

You can also use XMLHttpRequest object to transfer data between the browser and the server.

Sign up to request clarification or add additional context in comments.

12 Comments

probably worth pointing out that this solution requires jQuery, which the OP has not mentioned they are using - and they may not want to add it just for one ajax request. Why not just propose a solution using fetch()?
Thanks. Yeah right, it is worth mentioning. However JQuery fixed the problem.
What I mean is you should edit your answer to explain that fact, especially in case others read this in future. Answers on this site are not aimed only at the person who originally asked the question...
@Samet to improve the quality of the answer, would you explain to me why did you changed your mind?
@Hardood <?php //receive the value ( actif, pasif) $flag = $_POST['flag']; //database connection if( $flag == 1) //upadate active --actif else //update inactive --pasif //statement to update // response: echo "updated successfuly".. Can you explain this part a little more?
|
0

Php and Js Code;

<p id="<?=$araccek['arac_id']?>"></p>


<script>

function createCountDown(elementId, date, dateiki, durum) {

    var countDownDate = new Date(date).getTime();

var x = setInterval(function() {

    // Sistem saatini alıyoruz
    var now = new Date().getTime();
    
    // Geri sayımla şimdi saat arasındaki zamanı ölçüyoruz
    var distance = countDownDate - now;

    
    // Gün, Saat, Dakika, Saniye işlemleri yapılıyor
    var days = Math.floor(distance / (1000 * 60 * 60 * 24));
    var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
    var seconds = Math.floor((distance % (1000 * 60)) / 1000);
    
    // Anlık olarak id="gerisayim" içine aktarılıyor
    document.getElementById(elementId).innerHTML = "Süre ►" + days + ": " + hours + ": " + minutes + ": " + seconds;
    
    
    
    
    // Süre dolduğunda yazacak yazı.
    if (distance < 0) {
        clearInterval(x);

        document.getElementById(elementId).innerHTML = "Başladı !";
        
        $.ajax({
           Type:'POST',
            url:'modify.php',
            data:{durum:2},
             success:function(response)
             {
                 // do sth here like alert a msg
                   alert(response);
             }
      });


        
        
        var bitistarihi = new Date(dateiki).getTime();
        var y = setInterval(function() {
            var simdi = new Date().getTime();
            var distance = bitistarihi - simdi;
            var days = Math.floor(distance / (1000 * 60 * 60 * 24));
            var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
            var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
            var seconds = Math.floor((distance % (1000 * 60)) / 1000);
            document.getElementById(elementId).innerHTML = "Süre ►" + days + ": " + hours + ": " + minutes + ": " + seconds;
            if (distance < 0) {
                clearInterval(y);

                document.getElementById(elementId).innerHTML = "Bitti !";
                
                $.ajax({
           Type:'POST',
            url:'modify.php',
            data:{durum:1},
             success:function(response)
             {
                 // do sth here like alert a msg
                   alert(response);
             }
      });
                
                }
        }, 1000);
                
        
    }
}, 1000);

}

createCountDown("<?=$araccek['arac_id']?>", "<?=$araccek['arac_gerisayim']?>", "<?=$araccek['arac_tarih']?>", "<?=$araccek['arac_durum']?>")
  



</script> 

Modify.php;

<?php
      $flag = $_POST['durum'];
      $id = 1; //You have to receive the value of the id from the client. I mean to passed with the ajax request.
      $dsn = "mysql:host=localhost;dbname=sametsarihan_db";
      $pdo = new PDO($dsn, 'sametsarihan_db', '********');
       if( $flag == 1)
                $value = "Aktif";
        else
                 $value = "Pasif";

         $stm = $pdo->prepare("Update araclar set arac_durum=? WHERE arac_id = ?");
         $stm->bindValue(1, $value);
         $stm->bindValue(2, $id);
    if( $stm->execute())
       echo "updated successfuly";
     else
        echo "error";
        
        ?>

My Database Table:

https://hizliresim.com/9J87vg

15 Comments

Instead of $id = 1, it must be $id = $_POST['id']; In your ajax code modify data :{ flag:1} to data:{flag:1, id:value}. value must be the real value of the key in your table.
$id = $_POST['arac_id']; data: {flag: 1, id: elementId}, this way?
There will be no definite identity value. I add items with the foreach loop. No matter what item is currently added, it must be processed. I guess elementId provides this in js codes.
What is this : <p id="<?=$araccek['arac_id']?>"></p>
Try to add id parameter to ajax code like this: data:{flag:1,id:elementId},.......
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.