I have a delete button for deleting a record from a database, mostly delete should always have a confirmation. So when i click delete it would ask a confirmation if i really want to delete the record or not. How may i able to call a method from a controller to do the work of deleting the record by just redirecting on the page itself or just reloading the page after i confirm delete?
Here are few lines of codes to make it clear:
<li>
<p>My Record</p> <a href="javascript:confirmDelete(". <?php echo $arr['id']; ?> .")">Delete</a>
</li>
<script>
function confirmDelete(val){
var r = confirm("Are you sure you want to delete this record?");
if(r){
//do the action here by calling a method or something from the controller in order to delete the record
}
}
</script>
you can move the confirmation code to
onClickand set the actual url inhref(e.g.$this->baseUrl('record/delete/id/x')). If user clicks on cancel on the confirmation box, href will be ignore and if user clicks on ok on the confirmation box, he will be taken to the url which will delete the record.e.g.
<a href="<?php echo $this->baseUrl('record/delete' . $arr['id']); ?>" onclick="return confirm('Are you sure you want to delete?')">Delete</a>