I have a page with multiple items, and user can delete any of the items right there on the same page. But I need to integrate a javascript confirm alert box to authenticate if the user really wants to continue with the action for each item.
I was able to achieve it by placing the javascript in the page but the challenge is that I can only use just one javascript function to delete only one item. This simply means i would be having multiple javascript for each item and that is damn difficult because the number of these items cannot be defined as it keep increasing.
So I need to be able to use just one javascript function for all the items being displayed.
Here is the javascript code:
<script>
<!--
function confirmAlertBox(){
var retVal = confirm("Do you want to continue ?");
if( retVal == true ){
window.location = "items.php?del_item&item_id=<?php echo $item_id; ?>";
return true;
}else{
return false;
}
}
//-->
</script>
Here are the items being fetch from the database:
<?php
while ($all_item_row = @mysql_fetch_assoc($all_item_query)) {
$item_title = $all_item_row['title'];
$item_id = $all_item_row['id'];
echo '<tr> <td>'.$item_title.'</td> <td class="action_lnk"><a href="edit_item.php?edit_cal&item_id='.$item_id.'">Edit Item</a> <a href="#" onclick="confirmAlertBox()">Delete Item</a> <div class="clear"></div></td> </tr>';
}
}
else{
echo "<strong>No item available. Thank You!</strong>";
}
?>
I would remove the separate function all together and go with something a bit simpler.
When you are building your table cell, change the Delete link to:
Not sure how you escape the ‘ character in php, so you would need to do this in the confirm function
That way the link is in the anchor already and the user will be sent to this link if they click ok. Otherwise
falsewill be returned and the user will stay where they are.