Yii Ajax Submit button



PHP Snippet 1:

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

PHP Snippet 2:

 <?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')
        ); ?>

PHP Snippet 3:

<?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')
); ?>

PHP Snippet 4:

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