문제

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.

도움이 되었습니까?

해결책

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/

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top