As with the first successful query, run another query on the next element from the array?

php

$arr = array(1, 4, 6, 8, 9, 12); 

js

 $(function(){ $(".buton").click(function(){ var id = "<?php echo $arr[0]; ?>"; $.ajax({ url: "Ajax.php", type: "POST", data: {id: id}, success: function(html){ if (html == 1){ // Если запрос успешный и ответ равен == 1. // Вполняем следующий Ajax запрс. }else{ alert("Ошыбка !!!"); } } }); }); }); 
  • god, what a mess - zenden2k
  • Do you have php running on the client side? - Visman 1:21 pm
  • I php code cited for example what data is transmitted via ajax request. - user185375 pm
  • Maybe I didn’t correctly ask a question or explained it wrong, and then what a horror began, they started putting minuses. What kind of people. - user185375

1 answer 1

 var sendRequestEachValue = function (values) { var newValues = values, value = newValues[0]; newValues.splice(0, 1); // удаляем элемент который будем отправлять $.ajax({ type: 'get', url: 'url', data: { id: value }, success: function (res) { if (newValues.length > 0) { sendRequestEachValue(newValues); // если элементы в массиве остались, то отправим запрос еще раз } } }); }; $('.button').on('click', function () { var values = [1, 2, 3, 4, 5]; sendRequestEachValue(values); }); 
  • How to use this function by clicking on the .buton button; but does not work. - user185375
  • @saturn Updated the answer. Why does not it work? Maybe there are errors in the browser console? - JILeXanDR
  • @saturn put brekpoint. Is the function called at all or not? - arg