3

How can I add a JavaScript alert to confirm (yes or no) when the user clicks the delete button? I tried adding a class to an alert:

<?php
//$con = mysqli_connect("localhost", "root", "root", "db");
$sql = "SELECT * FROM `uploads` where userId = " . $_SESSION['user'];
$qry = mysqli_query($conn,$sql) or die(mysqli_error($conn));

$table_content = "";

while($row = mysqli_fetch_assoc($qry)){
    $id = $row['id'];
    $name = $row['name'];
    $table_content .= "<tr>
                         <td>
                           <a href='listen.php?id=$id' target='_new'>$name </a>
                         </td>
                         <td>
                           <a href='delete.php?id=$id' type='button' class='btn btn-danger'>delete</a>
                         </td>
                       </tr>";
}

echo "<table>".$table_content."</table>";
?>
Badacadabra
  • 8,043
  • 7
  • 28
  • 49
Philly Hanna
  • 31
  • 2
  • 5

5 Answers5

6

For simplicity check this out.

<a onClick="return confirm('Are you sure you want to delete?')" href='delete.php?id=$id' type='button' class='btn btn-danger'>delete</a>

return false is actually doing three very separate things when you call it:

  • event.preventDefault();
  • event.stopPropagation();
  • Stops callback execution and returns immediately when called.

Since confirm method returns true or false, we can simply call it on onclick then the return determines the action.

reference: kamesh answer

 <a onClick="return confirm('Are you sure you want to delete?')" href='delete.php?id=$id' type='button' class='btn btn-danger'>delete</a>
       
Community
  • 1
  • 1
Lekens
  • 1,823
  • 17
  • 31
1

I think thats what you are looking for.

<a href='delete.php?id=$id' onclick="return myFunction()" type='button' class='btn btn-danger'>delete</a>

<script>
function myFunction() {
var r = confirm("OK to delete?");
if (r == false) {
   return false;
} 

}
</script>
D. Pachauri
  • 244
  • 2
  • 8
0

If you are using Bootstrap, you can give this a whirl!

https://github.com/delboy1978uk/bs-delete-confirm

And if you aren't using bootstrap, give it a shot! Some great functionality ready to rock as soon as you include the css and js! This jQuery delete confirm code uses the Bootstrap modal, see an example here http://getbootstrap.com/javascript/#modals

<a href="http://nasa.gov" class="delete-confirm btn btn-primary">Go!</a>


$(document).ready(function(){
  $('.delete-confirm').deleteConfirm();
});

See a working Example HERE https://jsfiddle.net/myu3kr66/

delboy1978uk
  • 12,118
  • 2
  • 21
  • 39
0

you can use a modal and on click of delete button, show that modal, in the modal, keep two buttons, yes and no ans assign id to both of them like, #yes and #no respectively. on click of yes, perform the delete operation and on click of no, keep the row as it is.

<td><a href='listen.php?id=$id' target='_new'>$name </a></td>
<td><a href='delete.php?id=$id' type='button' id="delete"                             

class='btn btn-danger'>delete</a></td>
 </tr>";
}
echo "<table>".$table_content."</table>"

 <script>

$('#delete).on('click',function(){

 $('#modal').show();
 $('#yes').on('click') {
//   perform delete action.
 }
 }
</script>
pd1
  • 36
  • 7
0

Please follow

<a href="javascript:void(0)" onclick="return deleteContent('<?php echo $id; ?>');" type="button" class="btn btn-danger">Delete</a>

Script

<script>
    function deleteContent(id) {
        if(confirm('Are you sure you want to delete this ?')) {
        window.location='delete.php?id='+id;
        }
    return false;
    }
</script>

Updated

<?php
 $con = mysqli_connect("localhost", "root", "root", "db");
 $sql = "SELECT * FROM `uploads` where userId = " . $_SESSION['user'];
 $qry = mysqli_query($conn,$sql) or die(mysqli_error($conn));

 echo "<table>";
 while($row = mysqli_fetch_assoc($qry)){
      $id = $row['id'];
      $name = $row['name'];
?>
<tr>
<td><a href="javascript:void(0)" onclick="return deleteContent('<?php echo $id; ?>');" type="button" class="btn btn-danger">Delete</a></td>
<td><a href='listen.php?id=<?php echo $id; ?>' target='_new'><?php echo $name; ?> </a></td>
</tr>

<?php } echo "</table>"; ?>
Bachcha Singh
  • 3,850
  • 3
  • 24
  • 38