Sweetalert with load - check if it was executed (php - jquery - ajax - sweetalert)

Asked

Viewed 119 times

0

I want that after loading the PHP file reportas_excel.php close the swal, until this part is working.

The Problem is that when you finish loading it does not generate the file download(reportas_excel.php) xls.

Code - js: Function btnXLS() {

swal('Gerando arquivo XLS');
        swal.showLoading();
        jQuery.get("library/common/relatorios_excel.php", "act=relatorio-excel-prospect", function (data) {
            //console.log(data);
            swal.close();
        });

}

Code - PHP (reportas_excel.php): //File that generates the xls.

if (isset($_GET['ProspectPorPeriodoGerado'])) {
                $prospects = $prospectDAO->ListaProspectPorPeriodoGerado();
            } else {
                $prospects = $prospectDAO->ExecutaSQL($_SESSION["string_sql"]); //$_SESSION["string_sql"] - Sessão que armazena a query em table_prospects.php
            }
            $excel = "
            <table border=\"1\">
                <tr style=\"height: 150px;\">
                    <td style=\"width: 150px;\" align=center><img src='$logotipo'></td>                
                    <td colspan=8 style=\"text-align: center; vertical-align: middle; background-color: #ddd; color: #333;\"><font style=\"font-size: 25px;\"><b>$title</b></font></td>
                </tr>            
                <tr>
                    <td colspan=9 style=\"height: 50px; text-align: center; vertical-align: middle;\">
                        <font style=\"font-size: 18px;\"><b>RELATÓRIO DE PROSPECTS</b></font>
                    </td>
                </tr>                
                <tr>
                    <td style=\"height: 30px; text-align: left; vertical-align: middle; background-color: #ddd; color: #333;\"><b>CRIADO EM</b></td>
                    <td style=\"height: 30px; text-align: left; vertical-align: middle; background-color: #ddd; color: #333;\"><b>NOME</b></td>
                    <td style=\"height: 30px; text-align: left; vertical-align: middle; background-color: #ddd; color: #333;\"><b>TELEFONE</b></td>
                    <td style=\"height: 30px; text-align: left; vertical-align: middle; background-color: #ddd; color: #333;\"><b>E-MAIL</b></td>
                    <td style=\"height: 30px; text-align: left; vertical-align: middle; background-color: #ddd; color: #333;\"><b>RESPONSÁVEL</b></td>
                    <td style=\"height: 30px; text-align: left; vertical-align: middle; background-color: #ddd; color: #333;\"><b>INTERESSE</b></td>
                    <td style=\"height: 30px; text-align: left; vertical-align: middle; background-color: #ddd; color: #333;\"><b>GRUPO</b></td>          
                    <td style=\"height: 30px; text-align: left; vertical-align: middle; background-color: #ddd; color: #333;\"><b>ORIGEM</b></td>
                    <td style=\"height: 30px; text-align: left; vertical-align: middle; background-color: #ddd; color: #333;\"><b>ATENDENTE</b></td>
                </tr>
            ";
            while ($arrayProspect = mysql_fetch_array($prospects)) {
                //Nome
                if ($arrayProspect["nome"] == "") {
                    $arrayProspect["nome"] = "Não Informado";
                }
                $nome = $arrayProspect["nome"];
                //Telefone
                if ($arrayProspect["telefone"] == "") {
                    $arrayProspect["telefone"] = "Não Informado";
                }
                $telefone = $arrayProspect["telefone"];
                //E-mail
                if ($arrayProspect["email"] == "") {
                    $arrayProspect["email"] = "E-mail Pendente";
                }
                $email = $arrayProspect["email"];
                //Responsável / E-mail
                if ($arrayProspect["responsavel"] == "") {
                    $arrayProspect["responsavel"] = "Não Informado";
                }
                if ($arrayProspect["responsavel_email"] == "") {
                    $arrayProspect["responsavel_email"] = "<span>E-mail Pendente</span>";
                }
                $responsavelEmail = $arrayProspect["responsavel"] . "<br>" . $arrayProspect["responsavel_email"] . "";
                //Situação
                if ($arrayProspect["lead"] == 0) {
                    $situacao = "Outros";
                }
                if ($arrayProspect["lead"] == 1) {
                    $situacao = "Leads";
                }
                //Interesse
                $interesse = $oportunidadeDAO->oportunidadeInteresse($arrayProspect["id"]);
                $excel .= "
                <tr>
                    <td style=\"text-align: left; vertical-align: middle;\">" . $arrayProspect["criado_em"] . "</td>
                    <td style=\"text-align: left; vertical-align: middle;\">" . $nome . "</td>
                    <td style=\"text-align: left; vertical-align: middle;\">" . $telefone . "</td>
                    <td style=\"text-align: left; vertical-align: middle;\">" . $email . "</td>
                    <td style=\"text-align: left; vertical-align: middle;\">" . $responsavelEmail . "</td>
                    <td style=\"text-align: left; vertical-align: middle;\">" . $interesse . "</td>
                    <td style=\"text-align: left; vertical-align: middle;\">" . $arrayProspect["grupo"] . "</td>
                    <td style=\"text-align: left; vertical-align: middle;\">" . $situacao . "</td>
                    <td style=\"text-align: left; vertical-align: middle;\">" . $arrayProspect["nome_atendente"] . "</td>
                </tr>
                ";
            }
            $excel .= "
                <tr><td colspan=9></td></tr>                
                <tr><td colspan=9 style=\"font-size: 13px; background-color: #ddd; color: #333;\">$footer</td></tr>
            </table>
            ";
            $arquivo = "relatorio-excel-prospects.xls";
            header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
            header("Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT");
            header("Cache-Control: no-cache, must-revalidate");
            header("Pragma: no-cache");
            header("Content-type: application/x-msexcel");
            header("Content-Disposition: attachment; filename=\"{$arquivo}\"");
            header("Content-Description: PHP Generated Data");
            echo utf8_decode($excel);
            exit;

1 answer

0


(RESOLVED)

I don’t know if it’s the right way, but so far it’s working for me. Obs: If it is a better way to develop just leave your answers. thanks!

Code js:

swal('Gerando arquivo XLS');
        swal.showLoading();
        jQuery.get("library/common/relatorios_excel.php", "act=relatorio-excel-prospect", function (data) {
            //console.log(data);
            swal.close();
        });

This code is checking the file upload reportas_excel.php.

All I did was:

Before:

<a href="javascript:;" onclick="btnXLSProspectPorPeriodoGerado('<?php echo $filtro_array["filtrar"]; ?>')">
        <span class="icon-file"></span> XLS Por Periodo Gerado
     </a>

Afterward:

<a href="library/common/relatorios_excel.php?act=relatorio-excel-prospect&ProspectPorPeriodoGerado" onclick="btnXLSProspectPorPeriodoGerado('<?php echo $filtro_array["filtrar"]; ?>')">
                      <span class="icon-file"></span> XLS Por Periodo Gerado
                                    </a>

Like the Code js is checking if the file(url) was loaded, I just passed the direct link on href from the button, as the check will be performed if the file(url) has been loaded and at the same time the download will be loaded. with this after loading the load swal will close and after will start the download of xls.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.