Hello, this script works but not as it should, it puts the result for which I clicked into the console, and with each click a new script, and I want to be separated by a comma.
Help please implement

$(window).load(function() { //говорим скрипту что он сработает когда вся страница загрузится $('.btn.btn-primary.btn-lg.width').click(function(e) { //ловим клик $("#basket").show("slow"); // показываем как откроется форма var ID = $(this).data('id'); // берет элемент по которому кликнули $.ajax({ // сам ajax запрос url: "myscript.php", // обработчик на php data: { id: ID }, // данные которые передаются type: "POST", // метод }) .done(function(data) { //отладочный запрос по которому заносятся данные console.log(data); // выводим отладочную информацию }); }); }) 

enter image description here

  • one
    The preview tab shows the server’s response , not what you are sending - Grundy
  • that is, to fix in php? the response is the same - Koly
  • one
    @Koly at the moment you are watching not console output. - Trymount
  • one
    @Koly be clear about logic. For your question it is not clear what you want to get in the end. Select multiple items and click on the button to send them separated by commas? - Trymount
  • one
    @Koly logic does not agree with your code. in your example, you click on 1 element and get its data-id , i.e. It turns out you need at least two scripts: 1 - to add data-id elements to the array when you click on an element, 2 - to send a request when you click on the compare button - Alex

1 answer 1

 $(window).load(function() { //говорим скрипту что он сработает когда вся страница загрузится var ID = []; $('.test').click(function() { ID.push($(this).data('id')); console.log(ID); }); $('.btn.btn-primary.btn-lg.width').click(function(e) { //ловим клик $("#basket").show("slow"); // показываем как откроется форма console.log(ID); $.ajax({ // сам ajax запрос url: "myscript.php", // обработчик на php data: { id: ID }, // даные которые передатся type: "POST", // метод }) .done(function(data) { //отладочный запрос по которому заносятся данные console.log(data); // выводим отладочную информацию }); }); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="test" data-id="1">test1</div> <div class="test" data-id="3">test2</div> <div class="test" data-id="2">test3</div> <button class="btn btn-primary btn-lg width" type="button">сравнить</button>