I need to call a js function in my php but it's not working. Can someone tell me what I'm doing wrong? How can I do this in an easy way?
Thank you!
I have three files:
mail.php
Responsible for sending my $_POST stuff (it works fine). I call my javascript function to toggle a modal depending if the mail was send or not.
<?
...
$response = $sendgrid->send($email);
if ($response) {
echo '<script type="text/javascript">',
'confirmaEnvio();',
'</script>';
header('Location: /contato');
}
else {
echo '<script type="text/javascript">',
'falhaEnvio();',
'</script>';
header('Location: /contato');
}
?>
functions.js
Two functions, one toggle the modal with a text telling the mail was sent, one toggle the modal with a text telling the mail was not sent.
function confirmaEnvio () {
$("#modal").find(".modal-body").text("Mensagem enviada com sucesso!");
$("#modal").modal("show");
}
function falhaEnvio() {
$("#modal").find(".modal-body").text("Erro!");
$("#modal").modal("show");
}
view.html
Form sending data to mail.php using POST
<form name="contato" method="post" action="../php/mail.php">
<div class="form-group row">
<label for="email" class="col-sm-2 col-form-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" name="email" placeholder="Email" required>
</div>
</div>
<div class="form-group row">
<label for="name" class="col-sm-2 col-form-label">Nome</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="name" placeholder="Nome" required>
</div>
</div>
<div class="form-group row">
<label for="msg" class="col-sm-2 col-form-label">Mensagem</label>
<div class="col-sm-10">
<textarea class="form-control" name ="msg" rows="10" id="msg" placeholder="Mensagem" required></textarea>
</div>
</div>
<div class="form-group row">
<div class="col-sm-10">
<button type="submit" name="submit" class="btn">Enviar</button>
</div>
</div>
my modal, same file, view.html
<div class="modal fade" id="modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>