0

I have some code i've been working on to have a box disappear once you click a button without refreshing the whole page, I can't get over my current hurdle. My AJAX doesn't seem to be posting any data, as you can see from the code below, I have a confirmation pop up which works and does pop up and when you click ok it SHOULD POST the ID to another page where the SQL runs, but it doesn't and i've tried testing it with echoing the POST to see if it retrieves it and no luck.

Any ideas why it won't post the data?

P.s I've checked to make sure the function is correct but here it is so you can see what the user clicks, and the id is 100% there

 <button onclick='good(<?= $appRow['customer_appointments_id']; ?>)' class='w3-button w3-block w3-green w3-section'><i class='fa fa-check'></i></button>

AJAX

      function good(id){
      if (confirm("Are you sure you want to complete this appointment?")) {
          $.ajax({
             type: "POST",
             url: "scripts/lead/completeLeadTask.php",
             data: ({
                 id: id
             }),
             cache: false,
             success: function(html) {
                $(".delete_mem" + id).fadeOut('slow');
             }
         });

Scripts/lead/completeLeadTask.php

    echo $_POST['id'];

  $stmt = $dbh->prepare("UPDATE `customer_appointments` SET `customer_appointments_status` = 'c' WHERE `customer_appointments`.`customer_appointments_id` = ?")->execute([$_POST['id']]);
  $stmt = null;
4
  • can you get the id on button click????
    – KUMAR
    Oct 29, 2019 at 16:16
  • How would I test that?
    – Ben Smith
    Oct 29, 2019 at 16:16
  • function good(id){ alert(id);
    – KUMAR
    Oct 29, 2019 at 16:18
  • Yes, that works. I get the correct id
    – Ben Smith
    Oct 29, 2019 at 16:19

1 Answer 1

0

You're passing id to the completeLeadTask.php page but using $_POST['customer_appointment_id'] when updating the database.

I removed the parenthesis from the data in the ajax request.

function good(id){
      if (confirm("Are you sure you want to complete this appointment?")) {
          $.ajax({
             type: "POST",
             url: "scripts/lead/completeLeadTask.php",
             data: {
                 id: id
             },
             cache: false,
             success: function(html) {
                $(".delete_mem" + id).fadeOut('slow');
             }
         });
    }
}
7
  • I will update my question, but as you can see above the SQL I tried doing echo id and that won't print anything either
    – Ben Smith
    Oct 29, 2019 at 16:08
  • Updated, does that work? I believe the parenthesis in the "data" might have caused an issue.
    – SamNewton
    Oct 29, 2019 at 16:18
  • I've just tried replacing my code for yours and I don't get anything now, not even the confirm box so i've put it back to my old code
    – Ben Smith
    Oct 29, 2019 at 16:22
  • Well then use your code, but remove the ( ) in data: ({ id: id }). That's all I did, so any errors are from copying/pasting without looking.
    – SamNewton
    Oct 29, 2019 at 16:24
  • No luck I'm afraid, I appreciate the help but it didn't change anything, I still get the confirmation box but after I click 'Ok' nothing happens
    – Ben Smith
    Oct 29, 2019 at 16:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.