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 ? :(
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!