ajaxSubmitButton:Yii 中控制器操作未重定向

发布于 2025-01-06 02:27:19 字数 1322 浏览 0 评论 0原文

我正在使用 ajaxSubmitButton 将数据发布到我的控制器。然后,该控制器操作在事务中生成两个插入语句。一切都运转良好。我现在想要的是一旦事务完成就重定向到新视图,但我无法让它工作。

当我单击按钮时,交易已处理,但我仍停留在同一页面上。我还尝试在按钮内使用“更新”并包装页面的一部分,但没有任何内容更新。但这并不是我最终想要的,因为我想最终渲染一个全新的视图。

这是我所拥有的...

查看

 echo CHtml::ajaxSubmitButton('submit',          
        array('/player/mark'), 
        array(
                'type'=>'POST',
                'data' => array(...),
        ));

控制器

public function actionMark() 
{
        $connection = yii::app()->db;
        $transaction=$connection->beginTransaction();
        try 
        {       
                $connection = yii::app()->db;
                $sql1 = "INSERT ...";
                $command=$connection->createCommand($sql1);
                ...
                $command->execute();


                $connection = yii::app()->db;
                $sql2 = "INSERT ...";
                $command=$connection->createCommand($sql2);
                ...
                $command->execute();

                $transaction->commit();
                $this->redirect(array('manage'));  // THIS IS NOT WORKING
        }
        catch(Exception $e)
        {
                $transaction->rollBack();
                $this->refresh;
        }
}

I am using ajaxSubmitButton to POST data to my controller. That controller action then makes two insert statements in a transaction. All that is working just fine. What I want now is once the transaction is complete to be redirected to a new view but I can't get that to work.

When I click my button the transaction is processed but I remain on the same page. I have also tried using 'update' within the button and wrapping part of the page but none of the content updates. That is not ultimately what I am after though since I want to end up rendering a whole new view.

Here is what I have...

VIEW

 echo CHtml::ajaxSubmitButton('submit',          
        array('/player/mark'), 
        array(
                'type'=>'POST',
                'data' => array(...),
        ));

CONTROLLER

public function actionMark() 
{
        $connection = yii::app()->db;
        $transaction=$connection->beginTransaction();
        try 
        {       
                $connection = yii::app()->db;
                $sql1 = "INSERT ...";
                $command=$connection->createCommand($sql1);
                ...
                $command->execute();


                $connection = yii::app()->db;
                $sql2 = "INSERT ...";
                $command=$connection->createCommand($sql2);
                ...
                $command->execute();

                $transaction->commit();
                $this->redirect(array('manage'));  // THIS IS NOT WORKING
        }
        catch(Exception $e)
        {
                $transaction->rollBack();
                $this->refresh;
        }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

郁金香雨 2025-01-13 02:27:19

重定向不起作用,因为您使用了 ajax。

尝试以下代码:

echo CHtml::ajaxSubmitButton('submit',          
        array('/player/mark'), 
        array(
                'type'=>'POST',
                'data' => array(...),
                'success' => 'js:function(){window.location="your_url"}', 
        ));

将 *your_url* 替换为您想要重定向到的网址

Redirect not working because you using ajax.

Try this code:

echo CHtml::ajaxSubmitButton('submit',          
        array('/player/mark'), 
        array(
                'type'=>'POST',
                'data' => array(...),
                'success' => 'js:function(){window.location="your_url"}', 
        ));

Replace *your_url* to url you want redirect to

阳光的暖冬 2025-01-13 02:27:19

运行成功后应添加window.location js。
参考这个ajax提交按钮示例

echo CHtml::ajaxSubmitButton(
    'Submit',
    '/post/ajax',
    array(
       'type'=>'POST',
       'dataType'=>'json',
       'success'=>'js:function(data){
           if(data.result==="success"){
              window.location="your_url"
           }else{
             $("#some-container").html(data.msg);
           }
       }',
    )
);

更多Yii 示例

You should add window.location js after run success.
Reference to this ajax submition button example:

echo CHtml::ajaxSubmitButton(
    'Submit',
    '/post/ajax',
    array(
       'type'=>'POST',
       'dataType'=>'json',
       'success'=>'js:function(data){
           if(data.result==="success"){
              window.location="your_url"
           }else{
             $("#some-container").html(data.msg);
           }
       }',
    )
);

More Yii examples

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文