Good evening. I have a Test button on the site. How to do so, for example, 3 different people press it, they are assigned different indices. The first is 001. The second is 002. The third is 003. And these indices were written into an array. And when this array is full, that is, three entries, the function for example worked

echo "Π—Π°ΠΏΠΈΡΡŒ ΠΏΡ€ΠΎΡˆΠ»Π° ΡƒΡΠΏΠ΅ΡˆΠ½Π°"; 

    2 answers 2

    Here you have two subtasks. Uniquely identify a specific visitor (I understand there is no registration). Depending on the details of the task, use cookies or session variables. Write there a unique identifier if the cookie / variable does not exist.

    The second subtask is to generate a unique ID. Here, again, depends on the task. As an option - take the current time, Ip visitor and get a hash from these values. The probability of coincidence is small. The second option is to write the correspondence to the database (SQL or file).

    If visitors need to know about each other, it is necessary to record information in the database or file (you can still in shared memory, for example)

    So that everyone can see the inscription "Filled". You must provide either an update. The simplest: periodic page refresh. Correct - AJAX (what have the options)

    • The user will have his ID he knows. He sees the button and the form there. He enters his ID in the form, he is sent to the file test.php and written to the array in the zero cell. Then another user entered the site and entered his ID there, and the form again sends it to test.php and his ID is already recorded in 1 cell of the array, and the third one does so. Then comes a random array. And then, after the randomization, it is necessary to delete all records from the array so that it is again clean for records. - TheAs110z

    here ajax

     <script> $(window).load(function() { //Π³ΠΎΠ²ΠΎΡ€ΠΈΠΌ скрипту Ρ‡Ρ‚ΠΎ ΠΎΠ½ сработаСт ΠΊΠΎΠ³Π΄Π° вся страница загрузится var ID = [];//ΠΎΠ±Π±ΠΎΠ·Π½Π°Ρ‡Π°Π΅ΠΌ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΡƒΡŽ для массива $('.btn.btn-primary.btn-lg.width').click(function() {//Π»ΠΎΠ²ΠΈΠΌ ΠΊΠ»ΠΈΠΊ ID.push($(this).data('id'));//записываСм Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ ΠΊΠ»ΠΈΠΊΠΎΠ² Π² массив console.log(ID); });//Ρ‡Ρ‚ΠΎ Π²Ρ‹Π΄Π°Π΄Π΅Ρ‚ консоль Ρ‚ΠΎΡ‡Π½Π΅Π΅ масив $('.btn.btn-primary.btn-lg.width').click(function(e) { //Π»ΠΎΠ²ΠΈΠΌ ΠΊΠ»ΠΈΠΊ $("#basket").show("slow"); // ΠΏΠΎΠΊΠ°Π·Ρ‹Π²Π°Π΅ΠΌ ΠΊΠ°ΠΊ откроСтся Ρ„ΠΎΡ€ΠΌΠ° }); // ΠΏΠΎΠΊΠ°Π·Ρ‹Π²Π°Π΅ΠΌ ΠΊΠ°ΠΊ откроСтся Ρ„ΠΎΡ€ΠΌΠ° $('.compare_basket').click(function(e) { //Π»ΠΎΠ²ΠΈΠΌ ΠΊΠ»ΠΈΠΊ $(".compare_basket").hide("slow"); });// ΠΏΠΎΠΊΠ°Π·Ρ‹Π²Π°Π΅ΠΌ ΠΊΠ°ΠΊ откроСтся Ρ„ΠΎΡ€ΠΌΠ° $('#basket').click(function(e) { //Π»ΠΎΠ²ΠΈΠΌ ΠΊΠ»ΠΈΠΊ $(".compare_basket").show("slow"); console.log(ID); //Ρ‡Ρ‚ΠΎ Π²Ρ‹Π΄Π°Π΄Π΅Ρ‚ консоль $.ajax({ // сам ajax запрос url: "myscript.php", // ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊ Π½Π° php data: {id: ID}, // Π΄Π°Π½Ρ‹Π΅ ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ пСрСдатся type: "POST", // ΠΌΠ΅Ρ‚ΠΎΠ΄ success: function(response){ $("#ajaxDivId_1").html(response); $("#ajaxDivId_2").html(response.id_2); $("#ajaxDivId_3").html(response.id_3); }, // Ссли запрос ΡƒΠ΄Π°Ρ‡Π½Ρ‹ΠΉ Π²Ρ‹Π²ΠΎΠ΄ΠΈΠΌ Ρ‚ΠΎ Ρ‡Ρ‚ΠΎ Π²Π΅Ρ€Π½ΡƒΠ» скрипт php error: function(er){ alert(er); }, // Ссли запрос Π½Π΅ удался Π²Ρ‹Π²ΠΎΠ΄ΠΈΠΌ ΠΎΡˆΠΈΠ±ΠΊΡƒ }) .done(function(data) { //ΠΎΡ‚Π»Π°Π΄ΠΎΡ‡Π½Ρ‹ΠΉ запрос ΠΏΠΎ ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΌΡƒ заносятся Π΄Π°Π½Π½Ρ‹Π΅ // Π²Ρ‹Π²ΠΎΠ΄ΠΈΠΌ ΠΎΡ‚Π»Π°Π΄ΠΎΡ‡Π½ΡƒΡŽ ΠΈΠ½Ρ„ΠΎΡ€ΠΌΠ°Ρ†ΠΈΡŽ }); }); }) </script> 

     <?php header('Content-Type: application/json; charset=utf-8'); $idd = $_POST['id']; $result = count($idd); $id = array(); if (count($result) == 1) { $id_1 = (current($idd)); echo json_encode(array('id_1'=>$id_1)); } if (count($result) < 3) { next($idd); $id_2 = (current($idd)); echo json_encode(array('id_2'=>$id_2)); if ($result < 3) { $err[] = "МоТно ΡΡ€Π°Π½ΠΈΠ²Π°Ρ‚ΡŒ Π½Π΅ Π±ΠΎΠ»Π΅Π΅ Ρ‚Ρ€Π΅Ρ… модСль"; } elseif (count($err) > 4) { echo json_encode($err); } ?>} 
    html

     <div class="compare_basket" id="compare_basket"> <div class="container"> <div class="row"> <div class="col-md-4" id="ajaxDivId_1"></div> <div class="col-md-4" id="ajaxDivId_2"></div> <div class="col-md-4" id="ajaxDivId_3"></div> </div> </div> </div> <div class="basket" id="basket"> <div class="col-md-4 col-md-offset-8"> <button type="button" class="btn btn-lg btn-basket">Π‘Ρ€Π°Π²Π½ΠΈΡ‚ΡŒ Π²Ρ‹Π±Ρ€Π°Π½ΠΎΠ΅</button> </div> <button data-id=1 id='basket-block' val=1 class='btn btn-primary btn-lg width'> <span class='glyphicon glyphicon-user'> </span> <span class='glyphicon glyphicon-user'> </span>Π‘Ρ€Π°Π²Π½ΠΈΡ‚ΡŒ </button>