As a beginner, I've defined a snack bar function(snackbarfunction) and there are 3 elements which should be shown in different stages of the program(For instance,when user enters email the message 'Successful, Thanks' should be shown by snack bar) thus, I was going to separate elements by a variable from my PHP page. However, the variable isn't detectable either in JS or HTML part.
Is there any different way except the way I'm working on?
there is the form:
<?php
require_once "indexRequest.php";
?>
<form class="news-letter" id="email-form" method="post" action="indexRequest.php">
<div class="subscribe-hide">
<input class="form-control" type="email" id="subscribe-email" name="email" placeholder="Email Address" required>
<button onclick="snackbarfunction()" id="subscribe-submit" class="btn"><i class="fa fa-envelope"></i>
</button>
<span id="subscribe-loading" class="btn"> <i class="fa fa-refresh fa-spin"></i> </span>
<div id="snackbarrepeated">Email already exists.</div>
<div id="snackbardone">Successful, Thanks.</div>
<div id="snackbarfailed">Unsuccessful, Please try again.</div>
</div>
</form>
<br>
<p class="section-description">
We Will Notify You!
</p><!-- /.section-description -->
<br>
</div>
</div>
<br> <br>
</div>
<!-- /.container -->
</div>
<!-- /.pattern -->
</section>
function snackbarfunction() {
<?php if(!is_null($status)) { ?>
var x = document.getElementById("snackbarfailed");
x.className = "show";
setTimeout(function(){ x.className = x.className.replace("show", ""); }, 3000);
var y = document.getElementById("snackbarrepeated");
y.className = "show";
setTimeout(function(){ y.className = y.className.replace("show", ""); }, 3000);
var z = document.getElementById("snackbardone");
z.className = "show";
setTimeout(function(){ z.className = z.className.replace("show", ""); }, 3000);
<?php } ?>
}
</script>
<script>
// wait for the DOM to be loaded
$(document).ready(function() {
// bind 'email-form' and provide a simple callback function
$('#email-form').ajaxForm(function() {
});
});
</script>
</body>
</html>
and that's my PHP codes:
<?php
require_once "DB.php";
$status = null;
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$status = "s";
$email = $_POST['email'];
if(filter_var($email , FILTER_VALIDATE_EMAIL) && htmlspecialchars($_POST['email']))
{
$conn = connectToDB();
if( ! userGet($email , $conn))
{
userSave($email , $conn) ? $status = "Done" : $status = "Not-Done";
}
else
{
$status = "Duplicated";
}
}
}
?>
Thanks in advance for any help you are able to provide.