0

I have an "ic" value in the list and a predefined "og" value assigned to the button. Those values match in another table. I send to PHP file via ajax and I want it to confirm from the user the value in the result returned with ajax as a message. If cancel will return false and interrupt the process, but I don't know why couldn't succeed.

How can I handle confirm(sttMsg) and stop button action?

function statusMethod() {
  var sttMsg = "";

  jQuery.each(rows, function(rowid, row) {
    var ic = row.cat_a_req___task_status_raw;

    var og = 'close';
    jQuery.ajax({
      type: "POST",
      url: "/spc/statusCheck/check.php",
      data: {
        incoming: ic,
        outgoing: og
      },
      dataType: "json",
      success: function(data) {
        sttMsg = data.checkMsg;
      },
      async: false
    });
    return confirm(sttMsg);
  });
}

return statusMethod();
Fabian S.
  • 2,441
  • 2
  • 24
  • 34
  • **NB:** Synchronous AJAX requests are strongly discouraged. They cause your page/browser to freeze until the request has finished. You should rewrite your code to work with asynchronous requests instead. – Richard Deeming Nov 05 '21 at 14:35

1 Answers1

1

If I understand correctly, you question is about how to find out which button the user clicked in the confirm() dialog box and how to take an action based on that.

You need to save the result of the confirm() function to a variable and check its value, like this:

const result = confirm("What would you like to do?");
if (result) {
  console.log(`User confirmed. Value of result is ${result}`);
} else {
  console.log(`User canceled. Value of result is ${result}`);
}

In your code, it would be something like this:

function statusMethod() {
  var sttMsg ="";

  jQuery.each(rows, function(rowid, row) {
    var ic = row.cat_a_req___task_status_raw;

    var og = 'close';
    jQuery.ajax({
      type: "POST",
      url: "/spc/statusCheck/check.php",
      data: {incoming : ic, outgoing: og},
      dataType: "json",
      success: function (data) {
        sttMsg = data.checkMsg;
      },
      async: false
    });

    const r = confirm(sttMsg);
    if (r) {
      // Do something if user clicks OK
    } else {
      // Do something if the user clicks Cancel
    }
  });
}

return statusMethod();

You can read more about the confirm() function here: https://www.w3schools.com/jsref/met_win_confirm.asp

Another possible issue with your code is that you are not handling failures in the request. In that case, you would not have the sttMsg variable defined and your code would throw a ReferenceError when reaching the confirm(sttMsg) function call.

UPDATE

If you want to execute that when a button is clicked, you can do it this way:

function statusMethod() {
  var sttMsg = "Do you agree?";
  const r = confirm(sttMsg);
  if (r) {
    // Do something if user clicks OK
  } else {
    // Do something if the user clicks Cancel
  }
  console.log(`Result: ${r}`);
}
<button onClick="statusMethod()">Click me</button>
notrev
  • 665
  • 1
  • 5
  • 16
  • I need only true false value. statusMethod function working on buttonAction. for e.g. – Tan Çetin Nov 04 '21 at 14:34
  • @TanÇetin, I updated the response with an example of how to call a function from a button click, if that's what you were looking for – notrev Nov 08 '21 at 14:52