简体   繁体   中英

My AJAX isn't posting or responding when the user clicks submit

I am trying to have a list of events and on each event have a tick box and ax box, so one for marking the event as complete and the other as marking it as deleted. I want this to happen without refreshing the page and every time I click the button, nothing happens.

I think whats causing the issue is that I could have more than 1 event box, and they aren't unique but i'm unsure how to make sure they are unique so that the right event gets updated in my database. I do have separate files but they just contain my queries for it, the issue i've got is it's not even posting the data. It just does nothing when I click the box.

HTML/PHP

                <div class="w3-card w3-round w3-white w3-center delete_mem">
              <div class="w3-container">
                <p>Upcoming Event</p>
                <!-- <img src="/w3images/avatar6.png" alt="Avatar" style="width:50%"><br> -->
                <span><?= $appRow['customer_appointments_type']; ?></span>
                <p><?= $appRow['customer_appointments_datetime']; ?></p>
                <div class="w3-row w3-opacity">
                  <div class="w3-half">
                    <!-- <button class="w3-button w3-block w3-green w3-section" title="Complete"><i class="fa fa-check"></i></button> -->
                    <a id="<?= $appRow['customer_appointments_id']; ?>" class='w3-button w3-block w3-green w3-section btn-good'><i class='fa fa-check'></i></a>
                  </div>
                  <div class="w3-half">
                    <!-- <button class="w3-button w3-block w3-red w3-section" title="Delete"><i class="fa fa-remove"></i></button> -->
                    <a id="<?= $appRow['customer_appointments_id']; ?>" class='w3-button w3-block w3-red w3-section btn-danger'><i class='fa fa-remove'></i></a>
                  </div>
                </div>
              </div>
            </div>

Javascript/AJAX

            //Mark as deleted without refreshing the page
        $(document).ready(function() {
            $('.btn-danger').click(function() {
                var id = $(this).attr("id");
                if (confirm("Are you sure you want to delete this appointment?")) {
                    $.ajax({
                        type: "POST",
                        url: "scripts/lead/deleteLeadTask.php",
                        data: ({
                            id: id
                        }),
                        cache: false,
                        success: function(html) {
                            $(".delete_mem" + id).fadeOut('slow');
                        }
                    });
                } else {
                    return false;
                }
            });
        });

        //Mark as complete without refreshing the page
        $(document).ready(function() {
            $('.btn-good').click(function() {
                var id = $(this).attr("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');
                        }
                    });
                } else {
                    return false;
                }
            });
        });

First, as you mentioned, you shouldn't have different elements with the same ID. Change your code to something like this

Html

<button type="button" onClick='delete(<?= $appRow['customer_appointments_id']; ?>)' class='w3-button w3-block w3-red w3-section btn-danger'><i class='fa fa-remove'></i></button>

Then for the Javascript, write the function that will be called when someone clicks the delete button

Javascript

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM