user151715
user151715

Reputation:

PHP: Show yes/no confirmation dialog

My PHP page has a link to delete one MySQL table datum. I want it to be in such a way that when I click the 'delete' link a confirmation box should appear and it should ask "are you sure, you want to delete?", with two buttons: 'yes' and 'no'. When I click yes, I want to delete the MySQL table's data and when I click no, nothing should happen.

Upvotes: 22

Views: 229847

Answers (15)

BGS
BGS

Reputation: 1441

In php section:

if($_SERVER["REQUEST_METHOD"] == "POST"){
    if(isset($_POST["delete_plan"])){
    //deleting
    }
}

in html:

<form id="form_delete" style="display:none;" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
    Are you sure?
    <input type="submit" class="button" name="delete_plan" value="Yes"/>
    <input type="button" class="button" value="No" onClick="$('form#form_delete').hide();$('input#delete_confirm').show();"/>
</form>
<input id="delete_confirm"  type="button" class="button" value="Delete" onClick="$('form#form_delete').show(); $(this).hide();"/>

Form is hidden at 1st. On "Delete" button click form is displayed, Delete button hides. "No" button in the form reverts it back. "Yes" proceeds to delete, then page reloads and initial view returns, i.e. form is hidden, "Delete" button is not.

If your are dealing with many rows of data, you need to add a row id to form_delete and delete_confirm.

Upvotes: 0

Andy McRae
Andy McRae

Reputation: 667

I guess you could try something like this from this question:

function confirmation($link_delete,$link_keep,$message) ///confirm box pop up
    {
        echo "<script>javascript:
        var ask = confirm('".$message."');
        if(ask==true)
        {
            window.location ='".$link_delete."' 
        }
        else
        {
            window.location ='".$link_keep."'    
        }
        </script>";
    }

Upvotes: 0

Ankur prajapati
Ankur prajapati

Reputation: 527

You can handle the attribute onClick for both i.e. 'ok' & 'cancel' condition like ternary operator

Scenario: Here is the scenario that I wants to show confirm box which will ask for 'ok' or 'cancel' while performing a delete action. In that I want if user click on 'ok' then the form action will redirect to page location and on cancel page will not respond.

Adding further explanation i'm having one button with type="submit" which is originally use default form action of form tag. and I want above scenario on delete button with same input type.

So below code is working properly for me

onClick="return confirm('Are you sure you want to Delete ?')?this.form.action='<?php echo $_SERVER['PHP_SELF'] ?>':false;"

Full code

<input type="submit" name="action" id="Delete" value="Delete" onClick="return confirm('Are you sure you want to Delete ?')?this.form.action='<?php echo $_SERVER['PHP_SELF'] ?>':false;">

And by the way I'm implementing this code as inline in html element using PHP. so that's why I used 'echo $_SERVER['PHP_SELF']'.

I hope it will work for you also. Thank You

Upvotes: 1

Rohit Ghodadra
Rohit Ghodadra

Reputation: 7

<a onclick="javascript:return confirm('Are You Confirm Deletion');" href="delete_customer.php?a=<?php echo $row['id']; ?>"  class="btn btn-danger a-btn-slide-text" style="color: white; width:86px; height:37px;" > <span class="glyphicon glyphicon-remove" aria-hidden="true"></span><span><strong></a></strong></span> 

Upvotes: 1

Dai Zheng
Dai Zheng

Reputation: 9

Try this:

<td>
    <a onclick="return confirm(\'Are you sure?\');" href="'.base_url('category/delete_category/'.$row->category_id).'" class="btn btn-danger btn-sm"><i class="fa fa-trash"></i> Delete Coupon</a>
</td>

Upvotes: 0

elk
elk

Reputation: 687

I was also looking for a way to do it and figured it out like this using forms and the formaction attribute:

<input type="submit" name="del_something" formaction="<addresstothispage>" value="delete" />
<?php if(isset($_POST['del_something'])) print '<div>Are you sure? <input type="submit" name="del_confirm" value="yes!" formaction="action.php" />
<input type="submit" name="del_no" value="no!" formaction="<addresstothispage>" />';?>

action.php would check for isset($_POST['del_confirm']) and call the corresponding php script (for database actions or whatever). Voilà, no javascript needed. Using the formaction attribute, the delete button can be part of any form and still call a different form action (such as refer back to the same page, but with the button set).

If the button was pressed, the confirm buttons will show.

Upvotes: 0

Abhi Chavda
Abhi Chavda

Reputation: 81

<input type="submit" name="submit" value="submit" onclick="return confirm('Are you sure?');"/> you can perform this action on Button too!

Upvotes: 0

user1588255
user1588255

Reputation: 201

<input name="_delete_" type="submit" value="Delete" onclick="return confirm('Are you sure?')">

Upvotes: 19

Vishal Kumar
Vishal Kumar

Reputation: 4627

The best way that I found is here.

HTML CODE

<a href="delete.cfm" onclick="return confirm('Are you sure you want to delete?');">Delete</a>

ADD THIS TO HEAD SECTION

$(document).ready(function() {
    $('a[data-confirm]').click(function(ev) {
        var href = $(this).attr('href');
        if (!$('#dataConfirmModal').length) {
            $('body').append('<div id="dataConfirmModal" class="modal" role="dialog" aria-labelledby="dataConfirmLabel" aria-hidden="true"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button><h3 id="dataConfirmLabel">Please Confirm</h3></div><div class="modal-body"></div><div class="modal-footer"><button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button><a class="btn btn-primary" id="dataConfirmOK">OK</a></div></div>');
        } 
        $('#dataConfirmModal').find('.modal-body').text($(this).attr('data-confirm'));
        $('#dataConfirmOK').attr('href', href);
        $('#dataConfirmModal').modal({show:true});
        return false;
    });
});

You should add Jquery and Bootstrap for this to work.

Upvotes: 0

user2702359
user2702359

Reputation: 1

onclick="return confirm('Log Out?')? window.open('?user=logout','_self'): void(0);"

Did this script and afterwards i tought i go check the internet too.
Yes this is an old threat but as there seems not to be any similar version i thought i'd contribute.
Easiest & simplest way works on all browsers with/in any element or field.

You can change window.open to any other command to run when confirmation is TRUE, same with void(0) if conformation is NULL or canceled.

Upvotes: -1

Matthew Crumley
Matthew Crumley

Reputation: 102725

What I usually do is create a delete page that shows a confirmation form if the request method is "GET" and deletes the data if the method was "POST" and the user chose the "Yes" option.

Then, in the page with the delete link, I add an onclick function (or just use the jQuery confirm plugin) that uses AJAX to post to the link, bypassing the confirmation page.

Here's the idea in pseudo code:

delete.php:

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if ($_POST['confirm'] == 'Yes') {
        delete_record($_REQUEST['id']); // From GET or POST variables
    }
    redirect($_POST['referer']);
}
?>

<form action="delete.php" method="post">
    Are you sure?
    <input type="submit" name="confirm" value="Yes">
    <input type="submit" name="confirm" value="No">

    <input type="hidden" name="id" value="<?php echo $_GET['id']; ?>">
    <input type="hidden" name="referer" value="<?php echo $_SERVER['HTTP_REFERER']; ?>">
</form>

Page with delete link:

<script>
    function confirmDelete(link) {
        if (confirm("Are you sure?")) {
            doAjax(link.href, "POST"); // doAjax needs to send the "confirm" field
        }
        return false;
    }
</script>

<a href="delete.php?id=1234" onclick="return confirmDelete(this);">Delete record</a>

Upvotes: 4

jimyi
jimyi

Reputation: 31191

If you're deleting something, you should always use POST and not GET, so using confirm() is a bad idea. What I do in your situation is use a modal popup like jqModal and display a form with yes/no buttons inside the popup.

Upvotes: 0

xaddict
xaddict

Reputation: 1328

The PHP way of doing this would be using a dialog system inside php.for example GTKDialogs: http://www.kksou.com/php-gtk2/articles/setup-a-dialog-box---Part-2---simple-yes-no-dialog.php The javacript way is probably a bit easier, but remember when javascript is turned off, this does not work (except if you check javascript to be enabled and then add this!?) This could be with a onclick handler like tsvanharen posted, or with a simple text dialog inside the page instead of a nagging popup.

<a onClick="$('deletefromtable').show();"></a>
<div id="deletefromtable" style="display:none;">
 Do you really want to do this?<br/>
 <a href="deleteit.php">Yes</a>|<a onClick="$('deletefromtable').hide();">No</a>
</div>

I use prototype (hence the $() tag and the show()/hide()) for it. but you can easily change it to work without prototype:

<a onClick='document.getElementById("deletefromtable").style.display = "block";' href="#">click here to delete</a>
<div id="deletefromtable" style="display:none;">
 Do you really want to do this?<br/>
 <a href="deleteit.php">Yes</a>|<a onClick='document.getElementById("deletefromtable").style.display = "none";' href="#">No</a>
</div>

Again, this does not work without javascript, but almost no options do.

Upvotes: 0

Nick Clarke
Nick Clarke

Reputation: 1282

You can use JavaScript to prompt you:

Found this here - Example

<script>
function confirmDelete(delUrl) {
  if (confirm("Are you sure you want to delete")) {
   document.location = delUrl;
  }
}
</script>

<a href="javascript:confirmDelete('delete.page?id=1')">Delete</a>

Another way

<a href="delete.page?id=1" onclick="return confirm('Are you sure you want to delete?')">Delete</a> 

Warning: This JavaScript will not stop the records from being deleted if they just navigate to the final url - delete.page?id=1 in their browser

Upvotes: 14

Timothy S. Van Haren
Timothy S. Van Haren

Reputation: 8966

<a href="http://stackoverflow.com" 
    onclick="return confirm('Are you sure?');">My Link</a>

Upvotes: 59

Related Questions