0
I’m having problems trying to use this method of sending form by email, without refreshing the page. I don’t have much knowledge of Ajax yet, 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, but I couldn’t. The problem is that when you click the send button, no action occurs. The function is not called.
Code in Js:
$(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
How is the php file?
– Marlysson
I edited the question, and put how the php file is
– Felipe Evangelista
ta missing put a button like Submit (or Submit input) in your form
– Lauro Moraes
I edited there, when I copied the form to pass here, I forgot this input
– Felipe Evangelista
It is missing the ids in the inputs, being that you are catching them inside the ajax.
– Marlysson
Orra, true, I vacillate beast! kkk but anyway, clicking the button has no action
– Felipe Evangelista
$mail->ErrorInfo
returns something? Tbm putssmtpdebug = 1
to see if you’re making a mistake.– Max Rogério
It does not return anything. Is the right to call the function with onsubmit or action? With action has no action and with onsubmit gets the image error, even with smtpdebug = 1 and errorinfo returns nothing
– Felipe Evangelista