PHP: Show yes/no confirmation dialog

后端 未结 13 2080
爱一瞬间的悲伤
爱一瞬间的悲伤 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:58
    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.

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

    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.

    0 讨论(0)
  • 2021-01-30 19:02
    <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> 
    
    0 讨论(0)
  • 2021-01-30 19:03

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

    0 讨论(0)
  • 2021-01-30 19:07

    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

    0 讨论(0)
  • 2021-01-30 19:10

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