I'm having trouble trying to use this form submission method by email without refreshing the page. I still do not know much about Ajax, so I'm not sure if the code is correct, I even tried other methods, copying and pasting the code and only changing some things to see if it was going to, but I could not. The problem is that clicking the submit button does not take any action. The function is not called.
Js code:
$(function($) {
$("#form1").submit(function() {
var nome = $("#nome").val();
var email = $("#email").val();
var mensagem = $("#mensagem").val();
$("#status").html("<img src='loader.gif'/>");
$.post('email.php', {nome: nome, email: email, mensagem: mensagem }, function(envia) {
$("#status").slideDown();
if (envia != false) {
$("#status").html(envia);
}
else {
$("#status").html("Mensagem enviada com sucesso!");
$("#nome").val("");
$("#email").val("");
$("#mensagem").val("");
}
});
});
});
Form:
<form id="form1" class="row" method="post" action="javascript:function()">
<input type="text" placeholder="Nome:" class="form-control" name="nome"/>
<input type="email" placeholder="E-mail:" class="form-control" name="email"/>
<textarea placeholder="Mensagem:" class="form-control" name="mensagem"></textarea>
<input type="submit" class="btn btn-default btn-link" name="submit">
</form>
PHP
<?php
require_once("class/class.phpmailer.php");
date_default_timezone_set('America/Sao_Paulo');
$ip = getenv("REMOTE_ADDR");
$mail = new PHPMailer(true);
$mail->SetLanguage("br");
// $mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$nomeusuario = utf8_decode($_POST['nome']);
$emailusuario = utf8_decode($_POST['email']);
$mensagem = utf8_decode($_POST['mensagem']);
$mail->Host = 'email-ssl.com.br';
$mail->SMTPAuth = true;
$mail->Port = 465;
$mail->Username = '';
$mail->Password = '';
$mail->From = $emailusuario;
$mail->FromName = $nomeusuario;
$mail->AddAddress('', '');
$mail->Subject = "";
$mail->AddBcc($emailusuario);
$mail->Body = $mensagem;
if(!$mail->Send()) {
echo "Mensagem de erro: " . $mail->ErrorInfo;
} else {
echo "Mensagem enviada!";
}
?>
When I try to put onsubmit in the form, an error image appears