Phpmailer without ajax

Asked

Viewed 265 times

1

I have a form that I am passing via post to the phpmailer script, after sending it redirects to another page saying if the form was sent or not, but does not return the form page, I tried to do via ajax and could not:

Submit does not call function

would have some way to send the form by php and return the page only with php?

My form:

<div class="row contact">
<div class="col-lg-6 col-sm-6 ">

<form id="form" method="post" action="mail/phpmailer.php">
        <input type="text" class="form-control" placeholder="Nome completo:" name="nome">
        <input type="text" class="form-control" placeholder="E-mail:" name="email">
        <input type="text" class="form-control" placeholder="Número para contato:" name="num">
        <textarea rows="6" class="form-control" placeholder="Digite sua mensagem" name="msg"></textarea>
        <input type="button" class="btn btn-success" name="enviar" value="Enviar">
</form>
</div>
</div>

Configuration of Phpmailer:

<?php
require 'phpmailer/PHPMailerAutoload.php';

$mail = new PHPMailer();
$mail->setLanguage('pt');

$from = '[email protected]';
$fromName = '-';

$host ='smtp.umbler.com';
$username ='[email protected]';
$password ='-';
$port =587;
$secure =false;

$mail->isSMTP();
$mail->Host = $host;
$mail->SMTPAuth = true;
$mail->Username = $username;
$mail->Password = $password;
$mail->Port = $port;
$mail->SMTPSecure = $secure;

$mail->From = $from;
$mail->FromName = $fromName;
$mail->addReplyTo($from, $fromName);

$mail->addAddress('[email protected]', '-');

$mail->isHTML(true);
$mail->CharSet = 'utf-8';
$mail->WordWrap = 70;                            

$mail->Subject = 'Contato ';
$mail->Body    = 'Nome: ' .$_POST['nome'] . 'Email: ' . $_POST['email'] . 'Numero: ' . $_POST['num'];
$mail->AltBody    = $_POST['msg'];

$send = $mail->Send();

if($send)
    echo 'E-mail enviado com sucesso!';
else
    echo 'Erro : '.$mail->ErrorInfo;
?>

This way after pressing send it goes to the phpmailer.php page and informs whether it was sent or not, but does not return to the form page...

  • How do you set up email sending? You want it to redirect to some page depending on whether the email is sent or not?

  • I edited the question, see how this my code

4 answers

3

Add this to the end of your PHP by changing only the URL.

if($send){
  echo 'E-mail enviado com sucesso!';
  echo '<meta http-equiv="refresh" content="3;URL="paginadoformulario.HTML">';
}
else{
    echo 'Erro : '.$mail->ErrorInfo;
}

See if it solves your problem.

2


If your form is on a PHP page this is simple. You can do it with jQuery, but from what I understand, you want a simple solution.

<?php 

   if (isset($_GET['sucesso'])) {
  
   if ($_GET['sucesso']==1) {   ?>

        <div class="sucesso">Formulário enviado com sucesso!</div>

   <?php } else { ?>

        <div class="semsucesso">Erro enviando formulário</div>

    <?php 
    } 
}
?>


<div class="row contact">
<div class="col-lg-6 col-sm-6 ">

<form id="form" method="post" action="mail/phpmailer.php">
        <input type="text" class="form-control" placeholder="Nome completo:" name="nome">
        <input type="text" class="form-control" placeholder="E-mail:" name="email">
        <input type="text" class="form-control" placeholder="Número para contato:" name="num">
        <textarea rows="6" class="form-control" placeholder="Digite sua mensagem" name="msg"></textarea>
        <input type="button" class="btn btn-success" name="enviar" value="Enviar">
</form>
</div>
</div>

And in the PHP that sends the email change at the end:

if($send)
    header("Location: pagina-do-form.php?sucesso=1\n\n");    
else
    header("Location: pagina-do-form.php?sucesso=0\n\n");    

So it returns the form page (in PHP) and displays a DIV with the result (success or not).

  • I tried by ajax and broke my head too much because I didn’t have much knowledge about... I even have another question trying via ajax: http://answall.com/questions/135146/submit-n%C3%A3o-flame-fun%C3%A7%C3%A3o

  • I’ll try with your tip

  • I made the changes but the form page generated a Notice: Undefined index: success.

  • I changed the code a little. Sorry, the "Notice" errors are disabled on my server, so I didn’t see :-) Try now with the updated code. isset() will prevent this error.

  • I gave an improved last, now it should work 100%.

  • It worked, thank you!

Show 1 more comment

0

You can use the form itself

<form id="form-ajax" method="POST" action="sua pagina aqui">

There is a good tutoral on w3school

-2

if($send){
    echo 'E-mail enviado com sucesso!';
    sleep(1);
    echo '<script>window.history.go(-1)</script>';
} else {
    echo 'Erro : '.$mail->ErrorInfo;
}
  • Put this at the end of your code

  • Parse error: syntax error, Unexpected 'Else'

  • sorry, put it like this: } Else {

  • Actually you just add Sleep(1); and echo '<script>window.history.go(-1)</script>'; in your code

  • The wrong code ta, you forgot to put { after the ' if ($send) '.

  • worked, thanks, know if there is how I generate an alert to inform the user whether it was sent or not?

  • I put the keys where they were missing and it worked :)

  • beauty , the important thing is to make it work and understand :)

  • echo 'Email sent successfully! '; puts: echo "<script>Alert('Email sent successfully!');</script>";

Show 4 more comments

Browser other questions tagged

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