How to insert js array value to database

Hello, I have a program that is drawing random value from array, how can I add this drawn value to database, by clicking e.g. button?

This is not working example (because of js variable in it) which shows what I mean, I am showing it for better understanding.

bt.addEventListener("click", function() {
   <?php
      $stmt = $conn->prepare("INSERT INTO data (id, arrValue) VALUES (?, ?)");
      $stmt->bind_param('is', $id, arr[rand]); //arr[rand] is a js variable
   ?>
});

You’ll need to call some PHP code running on your server to be able to do what you want. Have a read up on Ajax.

Well, you have to send an AJAX request to server and that request will be post request and it contains the form data which you want to save in the database.

On the serverside, let’s say you are using PHP Laravel Framework then you will have to extract the data from the request and save that data into the database using either ORM or write the insert query or whatever your preferred method.

To submit post request from Javascript code, AJAX request is your only option.