简体   繁体   中英

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.

<a href="http://stackoverflow.com" 
    onclick="return confirm('Are you sure?');">My Link</a>
<input name="_delete_" type="submit" value="Delete" onclick="return confirm('Are you sure?')">

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

You should always use a form/post button to confirm something like this. Never rely on Javascript. Read this to understand why!

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>
<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> 

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.

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.

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.

<input type="submit" name="submit" value="submit" onclick="return confirm('Are you sure?');"/>你也可以在 Button 上执行这个操作!

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.

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>

You can handle the attribute onClick for both ie '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

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.

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