skip to Main Content

I want to save how many button print was click, and save it to database.

example:

<script>
count = 0;

function increment(){
count++;
return count;
}
</script>

<input type="hidden" name="count" id="count" />
<a href="#" onclick='increment()'>Print</a>

is this correct?

so when the Print clicked the count value will save to database

2

Answers


  1. Your code will increment the value of count each time Print is clicked but it then does nothing with the variable. (The return statement here does nothing.)

    This means it won’t endure beyond a page refresh. You need to then implement the code that saves it to a database or some kind of storage.

    Your question is tagged with a PHP tag but no accompanying code. You can certainly use php to save the data in say a MySql database or even just log it to a file.

    Have a look at https://www.php.net/manual/en/function.file-put-contents.php
    and https://www.php.net/manual/en/book.mysqli.php

    Hope that steers you in the right direction.

    Login or Signup to reply.
  2. <script>
    count = 0;
    
    function increment(){
    count++;
    // make xhr request to server and save count in db
    
    //return count;
    }
    </script>
    
    <input type="hidden" name="count" id="count" />
    <a href="#" onclick='increment()'>Print</a>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search