Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

212
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda