0
I have a little problem regarding the return of errors via ajax, follow the condigo:
Index.php
<h4>Cadastro de Nome</h4>
<form id="cadUsuario" method="post">
<label>Nome:</label><input required type="text" name="nome" id="nome" />
<br/><br/>
<input type="submit" value="Salvar" id="salvar" />
</form>
<script type="text/javascript">
$(function () {
$("#cadUsuario").submit(function (e) {
e.preventDefault();
var form_data = $(this).serialize();
$.ajax({
type: "POST",
url: "salvar.php",
dataType: "json", // Add datatype
data: form_data
}).done(function (data) {
var mensagem = '';
if (!data.insert){
mensagem = 'Falha no cadastro';
}
if(!data.email){
mensagem += 'Falha no envio do email';
}
if(data.insert && data.email){
mensagem = 'Operação realizda com sucesso';
}
alert(mensagem);
}, "json");
});
});
Save.php
<?php
include "config.php";
if((isset($_POST['nome'])&&$_POST['nome']!="")){
$nome = $_POST['nome'];
$sql = "INSERT INTO `nomes` (`idnomes`,`nomes`) VALUES (NULL,'$nome')";
require('mail_config.php');
$resultado = array('insert' => false, 'email' => false);
$resultado['insert'] = mysql_query($sql);
$resultado['email'] = sendMail("Testando o Mailer","Mensagem de teste do Mailer com Ajax".$nome."","[email protected]","Jefferson Androcles");
echo json_encode($resultado);
}
mail_config.php
<?php function sendMail($assunto,$msg,$destino,$nomeDestino) {
require_once('Mailer/class.phpmailer.php');
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = 'host';
$mail->Port = '587';
$mail->Username = '[email protected]';
$mail->Password = 'Senha';
$mail->From = '[email protected]';
$mail->FromName = 'AndroclesMail';
$mail->IsHTML(true);
$mail->Subject = utf8_decode($assunto);
$mail->Body = utf8_decode($msg);
$mail->AddAddress($destino,utf8_decode($nomeDestino) );
if(!$mail->Send()){
echo '<span>Erro ao enviar, favor entre em contato pelo e-mail [email protected]!</span>';
}else{
}
}
?>
Until then everything is working, except error 2 (there is => 2), I make the error purposely but does not return me anything, the other two, IE, 1 and 3 works normal, can help me? and wanted to know if there are any errors in jquery whether it is a gambiarra or not kkkk, Thank you!
O if
$MailEnv === false
shouldn’t it be separated? the other two if compare different things ... think about the scenario that Sert failed and the email was successfully sent.– rray
So, I’m thinking that now! I’ll test your answer below!
– JASL