Confirm href link before deleting
I plan on having a confirmation window before deleting the script from the database, only if the wrong line is selected.
Code for href
echo '<td align="center" valign="middle"><a href="delete.php?id='.$row["id"].'" class="confirmation"><img src="img/icon/delete.png"></a></td>';
Java script
<script type="text/javascript">
var elems = document.getElementsByClassName('confirmation');
var confirmIt = function (e) {
if (!confirm('Are you sure?')) e.preventDefault();
};
for (var i = 0, l = elems.length; i < l; i++) {
elems[i].addEventListener('click', confirmIt, false);
}
</script>
When the delete button is clicked, it just deletes the line, no confirmation box appears?
+3
source to share
3 answers
PHP
echo '<td align="center" valign="middle"><a data-href="delete.php?id='.$row["id"].'" href="javascript:;" onclick="confirmToDelete(this);"><img src="img/icon/delete.png"></a></td>';
JavaScript
<script>
function confirmToDelete(element){
if(!element) return false;
if(confirm('Are you sure?')) self.location.href=element.getAttribute('data-href');
}
</script>
0
source to share
try the following code. You can do it without external javascript. just add the function confirm
to onlick
your link event.
PHP
echo '<td align="center" valign="middle"><a href="delete.php?id='.$row["id"].'" class="confirmation" onclick="return confirm(\'Are you sure?\');"><img src="img/icon/delete.png"></a></td>';
0
source to share