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

214
Visualizações
function not defined php-js

I have this piece of code into my php file:

<?php
if(isset($_POST['submit'])){
     if($_POST['email'] != ''){
           $email = $_POST['email'];
           $query = "INSERT INTO user(email) VALUES('". $email . "')";
           $stmt = $db ->query($query);
           if($stmt){
                 echo "<script>displayPopup('success')</script>";
           }else{
                echo "";
           }
      }
  }
?>

My JS file is included before the closing tag and it's well charged. But I have error saying the function displayPopup is not defined

Any idea ? :(

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The main problem as far as I can tell is that the displayPopup function is being defined at the end of your document.

PHP being server-side and JS being client side, the conditions for echo'ing your script occur before the javascript is loaded/read.

This means that at the moment of that script being executed, the "displayPopup" function does not, in fact, exist.

Quickest way to test is to add the following code just before the end of your < /head> tag:

<script type="text/javascript">
    function displayPopup(msg){
        alert(msg);
    }
</script>

You can always obviously replace that function with whatever your actual displayPopup function is. An alternative to this is to use Ajax to submit your forms for a more seamless experience but the above should fix your issue.

I hope this helps!

over 4 years ago · Santiago Trujillo 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