简体   繁体   中英

Delete all data from MySQL table with button

I am currently trying to implement a 'Delete' button that clears a MySQL table of all its data. I am finding it much more difficult than the submit button was.

Researched examples all show how to do it by a row, but I just have a single 'Delete' button. I believe I am currently way off, as I was trying to come up with a solution based on the submit Ajax function.

Currently:

HTML

<button class="myButton" id="delete" type="delete">DELETE</button>

JavaScript (This is where my hangup is I think)

function deleteMessage() {

$.ajax({
        type: 'POST',
        url: 'info_message.php',
        async: false,
        //data: ,
        success: function(response) {
            if (response == "success") {
                successPopup("Successfully deleted message");
            }
            else {
                alert("Unable to delete message. " + response);
            }
        }
    });
}

PHP (or here???)

if (isset($_POST['deleteMessage'])) {
    $message = $_POST['message'];

    // Deletes all records in Announcement table
    $query = "DELETE from Announcement";
    if ($dbc->query($query) === FALSE) {
        echo "Error: " . $query . "<br>" . $dbc->error;
    }
    else {
        echo "success";
    }

    exit();
}

You looking for post data which you're not passing with your ajax call. Specifically you need to add deleteMessage and message which is the 2 post variables you are trying to read in your php script. Something like below

$.ajax({
        type: 'POST',
        url: 'info_message.php',
        async: false,
        data: { deleteMessage: true, message:"insert what message you want to pass" } ,
        success: function(response) {
            if (response == "success") {
                successPopup("Successfully deleted message");
            }
            else {
                alert("Unable to delete message. " + response);
            }
        }
    });
}

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