Sending Email Using Jquery Ajax without Refresh

Asked

Viewed 1,012 times

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 appearsinserir a descrição da imagem aqui

  • How is the php file?

  • I edited the question, and put how the php file is

  • ta missing put a button like Submit (or Submit input) in your form

  • I edited there, when I copied the form to pass here, I forgot this input

  • It is missing the ids in the inputs, being that you are catching them inside the ajax.

  • Orra, true, I vacillate beast! kkk but anyway, clicking the button has no action

  • $mail->ErrorInfo returns something? Tbm puts smtpdebug = 1 to see if you’re making a mistake.

  • 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

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.