Jhapz Mendoza
Jhapz Mendoza

Reputation: 53

how to add confirmation message before deleting an item using php

i wanna add a confirmation message when i wanna delete an item showed in my webpage that's connected to a database

<?php
if (isset($_GET['Pdelete'])) {
    $delid = sanitize($_GET['Pdelete']);
    $db->query("DELETE FROM products WHERE ID = $delid");
    header('Location: Archived.php');
}
?>


<a href="Archived.php?Pdelete=<?= $product['ID']; ?>" class="btn btn-xs btn-default"><i class="far fa-trash-alt"></i></a>

these codes work fine, all i need now is a confirmation message. TIA

Upvotes: 1

Views: 165

Answers (2)

Joseph_J
Joseph_J

Reputation: 3669

Your best option is to use javascript. I really like to use a jQuery library called Jquery Validator. This library is very convenient to use and has a lot of other very powerful tools for validating a input form. That would be my first choice.

If you are looking for a pure php way of doing this you can use something like this.

if(isset($_POST['yes'])){

    $delid = sanitize($_POST['yes']);
    $db->query("DELETE FROM products WHERE ID = $delid");
    header('Location: Archived.php');   

}

if(isset($_POST['no'])){

    header('Location: whereever.php');  //Redirect to whereever.

}

if(isset($_GET['Pdelete'])){

    echo
    '<form action=""  method="post" enctype="multipart/form-data">

        <p>Are you sure that you want to delete this item?</p>
        <input type="submit" name="yes" value="' . $_GET['Pdelete'] . '"> <input type="submit" name="no" value="No">

    </form>';
     exit();
}

Upvotes: 1

Lomesh Kelwadkar
Lomesh Kelwadkar

Reputation: 110

Use this code i think i have helped you.

<a href="Archived.php?Pdelete=<?=$product['ID'];?>" class="btn btn-xs btn-default" onclick="return confirm('Are you sure you want to delete this item?');"><i class="far fa-trash-alt"></i></a>

Upvotes: 0

Related Questions