Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

108
Views
Sweetalert AJAX fails

I tried implementing SweetAlert2 to my code, I tried to call a function from the trigger button unsuccessfully.

any idea how to fix this code in order for it to work?

    $(".delete").click(function () {

                    swal({

                        title: 'Are you sure?',
                        text: "You won't be able to revert this!",
                        type: 'warning',
                        showCancelButton: true,
                        confirmButtonColor: '#3085d6',
                        cancelButtonColor: '#d33',
                        confirmButtonText: 'Yes, delete it!'
                    }).then(function () {
                 delete_post($(this).attr("id"));

                        swal(
                            'Deleted!',
                            'Your file has been deleted.',
                            'success'
                        )


                })

            })
        });

the called function:

  function delete_post(id) { 
            var info = {"delete_post_id":id};
            $.post('index.php',
                info
            ).done(function( response )  {
                if(response["error"]) {
                    $('#error').html(response["error"]);
                }else{
                    window.location = "index.php";
                }
            });
        }
about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

type console.info(this); before then this row :

delete_post($(this).attr("id"));

i think this is your function in then.

about 4 years ago · Santiago Trujillo Report

0

As @Halil said, it's because of this in your callback from the alert, this does not refer to your button in the callback, it refers to the callback function itself.

Assign the id to a variable before you display the alert so that you can use it in the callback like so:

$(".delete").click(function() {

  console.log('Delete button has been clicked...');

  // get the id
  var id = $(this).attr("id"));

  swal({
    title: 'Are you sure?',
    text: "You won't be able to revert this!",
    type: 'warning',
    showCancelButton: true,
    confirmButtonColor: '#3085d6',
    cancelButtonColor: '#d33',
    confirmButtonText: 'Yes, delete it!'
  }).then(function() {

    console.log('Confirm button clicked...');

    // use the id we declared earlier
    delete_post(id);

    swal(
      'Deleted!',
      'Your file has been deleted.',
      'success'
    );

  });

});

function delete_post(id) {

  console.log('Deleting post...', id);

  var info = {
    "delete_post_id": id
  };

  $.post('index.php', info)
    .done(function(response) {

      console.log('Completed request...', response);

      if (response["error"]) {

        $('#error').html(response["error"]);

      } else {

        window.location = "index.php";

      }

    });
}

I also added some console.log debugging so you can see what's going on when you run it in your browser.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!