0

I am having problem with this AJAX code.

It shows a confirmation pop up window before executing the code and changing the data.

However, whether I confirm or delete, it pops up twice. So the data query is executed twice.

Here is the code:

            $('#approveOrder').on('click', function() {
                var requestNumber="<?php echo $request_number; ?>";
                confirmQuestion = confirm('You are about to approve order number: ' + requestNumber + ' confirm?')
                if (confirmQuestion) {
                    $.ajax({
                        url: "<?php echo base_url('orders_dashboard/approve'); ?>",
                        type: "POST",
                        data: {
                            requestNumber: requestNumber,
                            orderId: "<?php echo $order_list[0]['order_id']; ?>",
                            orderValue: "<?php echo $tentative; ?>"
                        },
                        success: function() {
                            location.reload()
                        }
                    })
                }else{
                    return false;
                }
ADyson
  • 57,178
  • 14
  • 51
  • 63
  • 4
    This code alone isn't sufficient to demonstrate the problem. We'll need more context. Perhaps you are somehow attaching the click event twice, or perhaps some other code triggered the click event as well. It's unclear. the above code only shows one Ajax request and there's nothing to give a clue about how it could end up running twice. – ADyson Jan 29 '21 at 08:05
  • It's probably not an issue related to ajax but that your click event is emitted twice – misha1109 Jan 29 '21 at 08:07
  • Have a read of https://stackoverflow.com/questions/20054889/button-onclick-function-firing-twice – Nigel Ren Jan 29 '21 at 08:07
  • If it is a form, you want to assign to the submit and preventDefault: `$('#approveOrderForm').on('submit', function(e) { e.preventDefault(); ...` – mplungjan Jan 29 '21 at 08:17

0 Answers0