1

In some specific case I need to call function action again in itself. First time is working fine but second time meaning calling in itself doesn't work. I am using it in and calling from the form element with parameters.

function action(order, type) {
  var xhttp;
  var type = type;
  var id;
  var order = document.getElementById(order).innerText;
  if (type != "start2") {
    id = prompt("Načtěte svůj QR kód.");
  }

  if (id.trim() != null || id.trim() != "") {
    xhttp = new XMLHttpRequest();
    xhttp.open("POST", "functionSmallCustomers.php", true);
    xhttp.onreadystatechange = function() {
      if (this.readyState == 4 && this.status == 200) {
        var str = this.responseText;

        if (str.trim() == '1') {
          location.reload();
        } else if (str.trim() == '2') {
          var result = confirm("Opravdu chcete pikovat další zakázku? Jednu už máte rozpikovanou.");

          if (result == true) {
            action(order, "start2");
          }
        } else {
          alert(str);
        }
      }
    };
  } else {
    alert("Nenačetli jste QR kód.");
  }

  xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xhttp.send("type=" + type + "&o=" + order + "&i=" + id);
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
Poutnik
  • 11
  • 1
  • 2
    `if (id.trim() != null || id.trim() != "")` should use `&&`, not `||`. See https://stackoverflow.com/questions/26337003/execute-block-if-a-variable-is-not-one-of-some-specific-values – Barmar Jun 03 '21 at 15:12
  • 2
    when you overide the function param `order`, when you pass it again its changed to the value of innerText of that dom element – Lawrence Cherone Jun 03 '21 at 15:12
  • 3
    What's the point of this: `var type = type;`? – Barmar Jun 03 '21 at 15:13
  • Bramar thanks I have changed it. Declaring variable and assigning variable type. Lawrence Cherone. Yes I understand that and I guess I should of named params of fuction somethign else. But In generel I need to call the function in himselft with same parameters except for type parameter where I need it to be "start2" – Poutnik Jun 03 '21 at 15:24
  • If the type is equal to "start2" then `id` will be `undefined`. – Pointy Jun 03 '21 at 19:12
  • Pointy you are right thanks. – Poutnik Jun 04 '21 at 05:12

0 Answers0