Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

279
Visualizações
PHP call Javascript function but don't wait response

I'm writing code in PHP where the response of an if condition calls a Javascript function with SweetAlert2. Basically, when a register is updated, the button calls a PHP function and this one call a Javscript function if it successfully runs. This javascript function is a SweetAlert2 which shows a message and should wait for a click on the "Ok" button to redirect the user to another URL.

The problem is the message appears really fast and doesn't wait to click; it closes by itself. The redirect is not working as well.

I already tried this javascript function from the PHP code and it is running fine.

How can I fix it?

PHP code

    function UpdateReg() {
      $Titulo =  $_REQUEST['f_titulo'];
      $Descricao = $_REQUEST['f_desc'];
      $Valor = $_REQUEST['f_valor'];
      $cod = $_GET['cod'];
    
    if ($GLOBALS['$conexao']->connect_error) {
        die("Falha de Conexão: " . $GLOBALS['$conn']->connect_error);
      }
      
      $sql = "UPDATE produtos set titulo = '$Titulo', descricao = '$Descricao', valor = '$Valor', WHERE cod_produto = $cod";
    
    
      if (mysqli_query($GLOBALS['$conexao'], $sql)) {
    
        echo "<script>ChangeOk('produto')";
          
      } else {
    
        echo "Erro: " . $sql . "<br>" . mysqli_error($GLOBALS['$conexao']);
      } 
      
      mysqli_close($GLOBALS['$conexao']);
    
    }
    
    }

Javascript

    function ChangeOk(tipo)
        {
          Swal.fire({
            title: "Feito!",
            text: "Registro alterado com sucesso",
            icon: 'success',
            showConfirmButton: true
            }).then((result) => {
              if (result.isConfirmed) {
                window.location = "home.php?tipo=" + tipo
              } 
          })
        }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

As PHP is a server-side language and Javascript is a client-side language you might run in to some problems with the timing. Your echo in php is the first line your document will generate. You'll see this when you check the sourcecode of the page in your browser.

I'm thinking the problem here might be that you call for ChangeOk('produto') too fast. Your library and javascript-function might not have been enterily loaded while you al ready call it from the echo with php. If that's the case it might help to tell Javascript it needs to run the call after all code is loaded.

echo "<script> window.addEventListener('load', ChangeOk('produto')); </script>";
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda