How to use zend delete action in confirmation popu

2019-07-23 04:23发布

I want to use a confirmation popup to delete my items, so this is my delete action :

public function deleteAction()
{       
    $id = (int) $this->params()->fromRoute('id', 0);
    $article = $this->getObjectManager()->find('\Application\Entity\Article', $id);
    if ($this->zfcUserAuthentication()->hasIdentity()) {

    if ($this->request->isPost()) 
    {

         $this->getObjectManager()->remove($article);
         $this->getObjectManager()->flush();

         return $this->redirect()->toRoute('blog');
    }
    }
    else
    {
        return $this->redirect()->toRoute('user');
    }

    return new ViewModel(array('article' => $article));
}

and this is my blog view where i have the delete link :

<a href="<?php echo $this->url('delete', array('action'=>'delete', 'id' => $articles->getId())) ?>" class="btn btn-default btn-lg" onclick="if (confirm('Are you sure?')) { document.location = this.href; } return false;" id="dialog">Delete Article</a>
                <?php endif;?>
                      <script type="text/javascript">
                      $(function() {
                        $( "#dialog:ui-dialog" ).dialog( "destroy" );

                        $( "#dialog-confirm" ).dialog({
                            resizable: false,
                            height:140,
                            modal: true,
                            buttons: {
                                "Are you sure": function() {
                                                        document.form.submit();
                                    $( this ).dialog( "close" );
                                },
                                Cancel: function() {
                                    $( this ).dialog( "close" );
                                }
                            }
                        });
                    });
                      </script>

the problem is when i press the link it's redirected to the delete.phtml view, what i want is to delete the item when i confirm the popup.

So please if someone has any solution i will be very appreciative :)

2条回答
小情绪 Triste *
2楼-- · 2019-07-23 04:40

You could use confirm method.

if (window.confirm('Are you sure you want to delete?'))
{
    window.location = "<?php echo $this->url('delete', array('action'=>'delete', 'id' => $articles->getId())) ?>"
}
else
{
    // do nothing
}

If you want to use jQuery UI dialog,

                        buttons: {
                            "Are you sure": function() {
                                window.location = "<?php echo $this->url('delete', array('action'=>'delete', 'id' => $articles->getId())) ?>"
                                $( this ).dialog( "close" );
                            },
                            Cancel: function() {
                                $( this ).dialog( "close" );
                            }
                        }
查看更多
何必那么认真
3楼-- · 2019-07-23 05:02

i know this an old question but it has help me. so i figure there must be others also looking for the answer. so here how i do it..

  1. go to http://adriancallaghan.co.uk/jquery-confirmation-box-thickbox/

  2. download and save the javascript into your 'js' folder (mine i put in 'js/azza/deletebox.js')

  3. then add link to the file in your view *.phtml file

    $this->inlineScript()->prependFile($this->basePath('js/azza/deletebox.js'));
    
  4. then edit your href as follows

    <a href="<?php echo $this->url('article', array('action' => 'delete', 
    'id' => $article->getId())); ?>" 
    title="<?php echo "Delete ".$article->getNama(); ?>"    
    id="dialog-confirm" class="dialog-confirm">Delete</a>
    
  5. very important to set both 'id' and 'class' as "dialog-confirm" to trigger the javascript function

  6. and you also need Jquery, and Jquery-UI for this script to work

that's it...

查看更多
登录 后发表回答