Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

213
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!