views:

65

answers:

2

Am doing a contact management site in codeigniter, i have a function which delete the contact based on id.

For example the page will have all contacts listed. And each contact will have a link saying delete near it. The link will be to the function passing the id, like:

www.site.com/index.php/action/delete/23

So i want a confirmation box to ask user, yes or no for each link. So if user press yes it will be deleted and otherwise nothing happens. Hope I'm clear.

+1  A: 

You could have a confirmation page with a form to post back to the same url. In the controller check whether the form has been submitted. If it has, delete the contact. If not, display the confirmation page.

function delete($id)
{
    if ($this->input->post('confirm'))
    {
        $this->contact_model->delete($id);
    }
    else
    {
        $contact = $this->contact_model->get_contact($id);
        $this->load->view('delete_confirm', array('contact' => $contact));
    }
}

If you don't like the idea of an extra page you could use some javascript to display a confirmation box and do an AJAX post if the user confirms.

Edit :

Just as an aside, what I'd avoid doing is implementing the delete through a HTTP GET. A spider or bot following the delete links could inadvertently delete all the contacts. Its better to use HTTP POST.

Stephen Curran
+3  A: 

You need a javascipt prompt:

Your JS

function confirm_delete(){
    var r=confirm("Are you sure?");
    if (r==true){
      //Do somthing
    }else{
      //cancel
    }
}

Then add an onclick event to your link.

<a href="#" onclick="confirm_delete();">Delete</a>

If you want something shiny and less intrusive may i suggest jquery with one of a multiple of confirm dialog plugins.

http://projectshadowlight.org/jquery-easy-confirm-dialog/

http://kailashnadh.name/code/jqdialog/

DRL