PHP: Show yes/no confirmation dialog

后端 未结 13 2078
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-30 18:27

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 \

相关标签:
13条回答
  • 2021-01-30 18:45
    <a href="http://stackoverflow.com" 
        onclick="return confirm('Are you sure?');">My Link</a>
    
    0 讨论(0)
  • 2021-01-30 18:52

    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.

    0 讨论(0)
  • 2021-01-30 18:52

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

    0 讨论(0)
  • 2021-01-30 18:53

    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.

    0 讨论(0)
  • 2021-01-30 18:55
    <input name="_delete_" type="submit" value="Delete" onclick="return confirm('Are you sure?')">
    
    0 讨论(0)
  • 2021-01-30 18:56

    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>
    
    0 讨论(0)
提交回复
热议问题