Yii Ajax提交按钮

时间:2014-02-09 15:45:39

标签: javascript php jquery ajax yii

Yii 1.1.14

我正在尝试弹出一个选择一年,然后获取所选年份的报告输出:

enter image description here

弹出窗口由CJuiDialog生成。 在CJuiDialog'里面,我有一个表格。 我第一次在表单上有一个正常的提交按钮,它工作正常。 但它没有关闭弹出窗口。 然后我发现了这篇文章:Yii - CJuiDialog Close on submit button click 提交表单的对话框按钮关闭对话框,但似乎不会发布到指定的URL。 我也直接在表单中尝试了ajaxSubmitButton()。 单击此按钮时的结果相同。 应用程序日志中没有任何JS错误。

这是我的观点:

<div class="reporting">
    <?php 
        $this->beginWidget('zii.widgets.jui.CJuiDialog',array(
            'id'=>'mydialog',
            'options'=>array(
                'title'=>'select year test',
                'autoOpen'=>false,
                'modal'=>true,
                'buttons' => array(
                    'Dialog Submit Button'=>'js:function(){
                        $.post(
                            $("#select-year-form").attr("action"),
                            $("#select-year-form").serialize(),
                            function(){$("#mydialog").dialog("close");}                     
                        );
                    }',             
                        array('text'=>Yii::t('app','reporting.select.close'),'click'=> 'js:function(){$(this).dialog("close");}'),
                ),
            ),
        ));
    ?>
    <div class="well">
        <?php echo     CHtml::beginForm(CHtml::normalizeUrl(array('/site/about')),'post',array('id'=>'select-year-form')); ?>
        <table class="contentheader">
            <tr>
            <td><?php echo CHtml::DropDownList('year', $lastyear, $yearslist, array('options'=>array($lastyear=>array('selected'=>true)) )); ?></td>
            </tr>
        </table>
        <br />
        <?php echo CHtml::ajaxSubmitButton('Form Ajax Submit Button',
                CHtml::normalizeUrl(array('/site/about')), 
                array('success'=>'function(){$("#mydialog").dialog("close");}'),
                array('name' => 'run', 'class' => 'btn btn-success')
        ); ?>
        <?php echo CHtml::endForm(); ?>
    </div>

    <?php 
        $this->endWidget('zii.widgets.jui.CJuiDialog');
        echo CHtml::link(Yii::t('app','app.menu.reporting.planning.xlsabsence'), '#', array(
            'onclick'=>'$("#mydialog").dialog("open"); return false;',
        ));
    ?>
</div>

编辑: 这是ajax提交按钮的请求标头:

Request URL:http://localhost/yii02/yiiars02/index.php?r=site/about
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4,de;q=0.2
Connection:keep-alive
Content-Length:9
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Cookie:b594784e48e60bab8dd41b584687725e=04rsh9hf09a4ef2f4a0in4prg7; e34b39f8afcdd208672f91abc90c8ad5=eqp4uva5819mj068b2n2qf3nu2; jpanesliders_panel-sliders=0; jpanesliders_position-icon=0; gantry-admin-tab=3; f6ea8ae4b9346c959ddbac3f5fefea4b=kk6g5oahbmql62rqkdiafem904; 60c5ada15aad7e760a944539ad24030d=bhf70j5o138vlse6m1gsmv49k6; 098c7ced18d50e0c9e49b567bc9f0832=fr-FR; 160a24a8bbe9b9a4500e829a48ae3415=e72bkntgosf81tbg26s6nk39j6; 25c0d40cd6ace920e960f397705968e5=pdgqudpu2psp5dpag72tol5tp0; PHPSESSID=31sij28hcolr3r1ip39rugmf62
Host:localhost
Origin:http://localhost
Referer:http://localhost/yii02/yiiars02/index.php?r=planning/test
User-Agent:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36
X-Requested-With:XMLHttpRequest
Query String Parametersview sourceview URL encoded
r:site/about
Form Dataview sourceview URL encoded
year:2014

响应标题查看来源

Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection:Keep-Alive
Content-Length:4810
Content-Type:text/html
Date:Mon, 10 Feb 2014 09:56:39 GMT
Expires:Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive:timeout=5, max=100
Pragma:no-cache
Server:Apache/2.2.17 (Win32) PHP/5.4.21
X-Powered-By:PHP/5.4.21

对于其他按钮,它具有相同的请求标头。 没有出现错误 - 为什么我看不到页面r = site / about? (实际上r = site / about只是一个测试页面,当我解决了这个问题时,我将不得不做目标页面。)

2 个答案:

答案 0 :(得分:0)

Ajax调用是针对页面上的部分更新而不是整个页面进行的。首先,您必须为要更新的元素定义id。例如

<div id='myDiv'>
</div>

现在你ajaxSubmitButton代码写

 <?php echo CHtml::ajaxSubmitButton('Form Ajax Submit Button',
                CHtml::normalizeUrl(array('/site/about')), 
                array('success'=>'function(){$("#mydialog").dialog("close");}',
                      'update'=>'#myDiv'                            ),
                array('name' => 'run', 'class' => 'btn btn-success')
        ); ?>

收到响应后,相应更新要更新的ID。请记住,您可以通过为其设置ID来更新任何元素。

答案 1 :(得分:0)

我完成了该过程,但结果是Excel无法读取的下载文件:

我介绍了这个ajax提交按钮:

<?php echo CHtml::ajaxSubmitButton('Form Ajax Submit Button',
    CHtml::normalizeUrl(array('/planning/xlsAbsences')), 
    array(
        'type'=>'POST',
        'beforeSend' => "function(request) { console.log('beforeSend'); }",
        'data'=>'js:$("#select-year-form").serialize()+"&year="+$("#dropDownId :selected").text()',
        'success'=>'function(response, status, request){    
            $("#mydialog").dialog("close"); 
            var disp = request.getResponseHeader("Content-Disposition");
            if (disp && disp.search("attachment") != -1) {
                var filename = disp.substring(21).replace("\"","");
                var form = $("<form method=\"POST\" action=\"index.php?r=planning/dl\">");
                form.append($("<input type=\"hidden\" name=\"content\" value=\"" + request.responseText + "\">"));
                form.append($("<input type=\"hidden\" name=\"filename\" value=\"" + filename + "\">"));
                $("body").append(form);
                form.submit();
            }
            console.log(filename);
            console.log(request.responseText);
        }',
        'complete' => "function(request) { console.log(request); }",
        'error' => "function(data) { alert('erro'+data); }",
    ),
    array('name' => 'run', 'class' => 'btn btn-success')
); ?>

我创建了一个新动作作为表单动作的目标 - 它叫做actionDl:

public function actionDl()
{
    $filename = $_POST['filename'];
    $content = $_POST['content'];
    header("Content-Disposition: attachment; filename=".$filename);
    header('Content-Type: application/octet-stream');
    header('Cache-Control: max-age=0');
    echo $content;
}

我认为request.responseText包含excel文件的内容,但也许我错了?