Sweetalert follow execution

Asked

Viewed 442 times

1

I have the confirm below using SweetAlert. If the user clicks the cancel it for the run and goes back to the previous page correctly. But if he clicks the continue, he does nothing. How can I make it follow the execution of the script by clicking on continue?

Detail: is not inside the isConfirm but rather "outside" it because it has other functions below it....

               sweetAlert({
                  title: "Tem certeza?",
                  text: "KM muito alta: <?php echo $km_retorno - $km_inicial; ?>. Se estiver errada pressione Cancelar!",
                  type: "warning",
                  showCancelButton: true,
                  confirmButtonClass: 'btn-danger',
                  confirmButtonText: 'Continuar',
                  cancelButtonText: "Cancelar",
                  closeOnConfirm: false,
                  closeOnCancel: false
                  },
                  function (isConfirm) {
                   if (isConfirm) {
                    //aqui deveria sair daqui e seguir a execução
                   } else {
                     sweetAlert({
                       title: "Erro!",
                       text: "Operação cancelada!",
                       type: "error"
                     },
                     function () {
                       window.location.href = 'InformaKM.php';
                    });
                  }
               });

UPDATE

As requested, below this alert follows this code. I know that is not the correct way to call the SweetAlert, but I’m correcting that.

    //TESTA SE FOI TENTADO PASSAR O FORMULÁRIO EM BRANCO (UM CAMPO É SUFICIENTE PARA TESTE, POIS TODOS SÃO DEPENDENTES
    if  (empty($km_inicial) || ($km_inicial == 0)) {
                ?>
                  <html>
                    <head>
                      <script src="sweet/dist/sweetalert.min.js"></script>
                      <link rel="stylesheet" type="text/css" href="sweet/dist/sweetalert.css">
                    </head>
                    <body>
                      <script>
                       sweetAlert({
                             title: "Erro!",
                             text: "Operação não permitida!",
                             type: "error"
                          },
                          function () {
                            window.location.href = 'InformaKM.php';
                       });
                      </script>
                    </body>
                   </html>
                <?
                exit;
    }

    //TESTA SE FOI TENTADO PASSAR KM NA SAIDA DA EMPRESA.
    if  (!empty($km_retorno) && (!empty($update))) {
                ?>
                  <html>
                    <head>
                      <script src="sweet/dist/sweetalert.min.js"></script>
                      <link rel="stylesheet" type="text/css" href="sweet/dist/sweetalert.css">
                    </head>
                    <body>
                      <script>
                       sweetAlert({
                             title: "Erro!",
                             text: "KM somente deve ser informada no retorno!",
                             type: "error"
                          },
                          function () {
                            window.location.href = 'InformaKM.php';
                       });
                      </script>
                    </body>
                   </html>
                <?
                exit;
    }
    /*
     *
     * INICIA O TESTE PARA SABER SE SERÁ FEITO UPDATE OU INSERT
     *
     */

    /*
     * Verifica se é saida da empresa
     *
    */

    if (!empty($update)) {

        /* VERIFICA SE A PORTARIA NÃO REGISTROU ALGUM OUTRO RETORNO DO
        *  MESMO VEICULO NO MESMO DIA OU SE ESTÁ REGISTRANDO A SAIDA DE
        *  RESERVA MAIS DE UMA VEZ
        */
        $query = "SELECT COUNT(*) as flag FROM RESERVAS
                  WHERE
                  DATA_RETORNO_REAL = '0000-00-00' AND
                  ATIVA = '1' AND
                  COD_VEICULO = :veiculo AND
                  DATE_FORMAT(DATA_SAIDA, '%d-%m-%Y') = :data_reserva AND
                  KM_INICIAL = :km_ini and
                  km_inicial > 0 and
                  ultimo_editor = 'portaria'";

        $data = $conexao->prepare($query);    // Prepara a query para execução
        $data->bindParam( ':km_ini', $km_inicial);
        $data->bindParam( ':veiculo', $veiculo);
        $data->bindParam( ':data_reserva', $data_reserva);
        $data->execute();// Executa(run) a query
        $data2 = $data->fetch();
        $flag = $data2['flag'];
   if ($flag == 0)  { //SE NÃO TEM RESERVA ABERTA OU SAIDA DUPLICADA

       $sql = "UPDATE reservas r SET r.km_inicial = :km_inicial, ultimo_editor = :user, ultima_atualizacao = now()  WHERE r.cod = :reserva";
       $stmt = $conexao->prepare( $sql );
       $stmt->bindParam( ':km_inicial', $km_inicial);
       $stmt->bindParam( ':user', $username);
       $stmt->bindParam( ':reserva', $reserva);

       $result = $stmt->execute();

      //VERIFICA SE RETORNOU ERRO
      if ( ! $result ) {
        var_dump( $stmt->errorInfo() );
        exit;

      } else { //SE INSERIU CORRETAMENTE
            ?>
              <html>
                <head>
                  <script src="sweet/dist/sweetalert.min.js"></script>
                  <link rel="stylesheet" type="text/css" href="sweet/dist/sweetalert.css">
                </head>
                <body>
                  <script>
                   sweetAlert({
                         title: "Sucesso!",
                         text: "Saida da Empresa Confirmada! Nº <? print($reserva); ?>.",
                         type: "success"
                      },
                      function () {
                        window.location.href = 'InformaKM.php';
                   });
                  </script>
                </body>
               </html>
            <?
            return;
      } //FECHA ELSE INSERÇÃO

     } ELSE { //SE A SAIDA DA EMPRESA JÁ ESTÁ REGISTRADA OU TEM OUTRO REGISTRO ABERTO DO MESMO VEICULO.
            ?>
              <html>
                <head>
                  <script src="sweet/dist/sweetalert.min.js"></script>
                  <link rel="stylesheet" type="text/css" href="sweet/dist/sweetalert.css">
                </head>
                <body>
                  <script>
                   sweetAlert({
                         title: "Erro!",
                         text: "Existem registros abertos para esse veiculo.\nSistema bloqueado!",
                         type: "error"
                      },
                      function () {
                        window.location.href = 'js/logout.php';
                   });
                  </script>
                </body>
               </html>
            <?
            exit;
} //FECHA ELSE VERIFICAÇÃO DE RESERVA ABERTA OU SAIDA DUPLICADA

/*
 *
 * É RETORNO para a empresa
 *
 *
*/
} ELSE {

    //TESTA SE ESTA INFORMANDO KM EM UMA RESERVA QUE NÃO SAIU AINDA.
    $query = "select r.km_inicial from reservas r where r.ativa='1' and r.cod  = '".$reserva."'";
    $data = $conexao->prepare($query);
    $data->execute();
    $row=$data->fetch();

    if ($row['km_inicial'] == 0) {  //SE A SAIDA ESTÁ CORRETA FAZ O UPDATE COM A KM DA ULTIMA SAIDA.

            ?>
              <html>
                <head>
                  <script src="sweet/dist/sweetalert.min.js"></script>
                  <link rel="stylesheet" type="text/css" href="sweet/dist/sweetalert.css">
                </head>
                <body>
                  <script>
                   sweetAlert({
                         title: "Erro!",
                         text: "Reserva não saiu da empresa. Verifique!",
                         type: "error"
                      },
                      function () {
                        window.location.href = 'InformaKM.php';
                   });
                  </script>
                </body>
               </html>
            <?
            exit;
    }

   $sql = "UPDATE reservas r set r.km_final = :km_retorno, r.data_retorno_real = curdate(), r.hora_retorno_real = curtime(), ultimo_editor = :user, ultima_atualizacao = now() WHERE r.cod = :reserva";
   $stmt = $conexao->prepare( $sql );
   $stmt->bindParam( ':km_retorno', $km_retorno);
   $stmt->bindParam( ':reserva', $reserva);
   $stmt->bindParam( ':user', $username);

   $result = $stmt->execute();

   //VERIFICA SE RETORNOU ERRO
   if ( ! $result )      {
        var_dump( $stmt->errorInfo() );
        exit;

   } else { //SE INSERIU CORRETAMENTE

            ?>
              <html>
                <head>
                  <script src="sweet/dist/sweetalert.min.js"></script>
                  <link rel="stylesheet" type="text/css" href="sweet/dist/sweetalert.css">
                </head>
                <body>
                  <script>
                   sweetAlert({
                         title: "Sucesso!",
                         text: "Retorno Nº <? print($reserva); ?> confirmado!",
                         type: "success"
                      },
                      function () {
                        window.location.href = 'InformaKM.php';
                   });
                  </script>
                </body>
               </html>
            <?

   } //FECHA ELSE INSERÇÃO

} //FECHA ELSE PRINCIPAL
?>
  • Here it worked correctly, I put an Alert inside the if of isConfirm

  • @Juniornunes yes, it performs whatever is inside isConfirm, but I need it to "exit" when clicked on continue, because it has other functions below it, understand?

  • Try calling one of the functions that are below within isConfirm, I think it works.

  • @Fred he is inside a PHP, he really need to "quit" to continue. It has variable tests, other Alerts and finally an update...

  • @Diego as well as "leave", and what exactly is below the sweetAlert? you can’t put the code block below inside a function and call in isConfirm? You can also close sweetAlert by confirming by changing it here: closeOnConfirm: false for true

  • 1

    But if you have things below this sweetalert, it will run even clicking or not on "Confirm", javascript will not expect anything to run from sweetalert. He will be signing. The best way to do it is by creating a Function and putting it inside isConfirm and also you have to change the closeOnConfirm to true (as @Juniornunes wrote), so your Alert will be closed. If it really isn’t possible to do it the way we’re talking, post the rest of your code or an example very close to what you want to think otherwise. So it’s not hard to help you

  • I added the code below Alert...

Show 2 more comments
No answers

Browser other questions tagged

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