Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在确认弹出窗口中使用zend delete操作?_Php_Jquery_Zend Framework_Doctrine Orm_Zend Framework2 - Fatal编程技术网

Php 如何在确认弹出窗口中使用zend delete操作?

Php 如何在确认弹出窗口中使用zend delete操作?,php,jquery,zend-framework,doctrine-orm,zend-framework2,Php,Jquery,Zend Framework,Doctrine Orm,Zend Framework2,我想使用确认弹出窗口删除我的项目,因此这是我的删除操作: public function deleteAction() { $id = (int) $this->params()->fromRoute('id', 0); $article = $this->getObjectManager()->find('\Application\Entity\Article', $id); if ($this->zfcUserAuthent

我想使用确认弹出窗口删除我的项目,因此这是我的删除操作:

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));
}
这是我的博客视图,其中有删除链接:

<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>

$(函数(){
$(“#dialog:ui dialog”).dialog(“销毁”);
$(“#对话框确认”)。对话框({
可调整大小:false,
身高:140,
莫代尔:是的,
按钮:{
“您确定”:函数(){
document.form.submit();
$(此).dialog(“关闭”);
},
取消:函数(){
$(此).dialog(“关闭”);
}
}
});
});
问题是,当我按下链接时,它被重定向到delete.phtml视图,我想在确认弹出窗口时删除该项目


所以,如果有人有任何解决方案,我将非常感激:)

您可以使用
确认方法

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(window.confirm('确定要删除吗?'))
{
window.location=“”
}
其他的
{
//无所事事
}
如果要使用jQuery UI对话框

                        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" );
                            }
                        }
按钮:{
“您确定”:函数(){
window.location=“”
$(此).dialog(“关闭”);
},
取消:函数(){
$(此).dialog(“关闭”);
}
}

我知道这是一个老问题,但它对我有帮助。所以我想肯定还有其他人在寻找答案。下面是我怎么做的

  • 下载javascript并将其保存到您的“js”文件夹中(我的文件放在“js/azza/deletebox.js”中)

  • 然后在视图*.phtml文件中添加指向该文件的链接

    $this->inlineScript()->prependFile($this->basePath('js/azza/deletebox.js'));
    
  • 然后编辑你的href,如下所示

    <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>
    
    
    
  • 将“id”和“class”都设置为“dialog confirm”以触发javascript函数非常重要

  • 您还需要Jquery和jQueryUI才能让这个脚本正常工作


  • 就这样

    这个问题的答案是@emaillenin提到的,
    document.form.submit()行并在
    $(此)对话框(“关闭”)后添加
    window.location.href=..