PHPMailer without ajax

1

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

  

Submit does not call function

would you have any way to submit 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>

PHPMailer Configuration:

<?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;
?>

In this way after pressing on send it goes to the phpmailer.php page and informs if it was sent or not, but does not return to the form page ...

    
asked by anonymous 16.06.2016 / 18:48

4 answers

2

If your form is in a PHP page this is simple. It's about 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 PHP sending 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).

    
16.06.2016 / 19:26
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.

    
16.06.2016 / 19:21
0

You can use form itself

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

There is a good tutorial on forms in w3school

    
16.06.2016 / 18:52
-2
if($send){
    echo 'E-mail enviado com sucesso!';
    sleep(1);
    echo '<script>window.history.go(-1)</script>';
} else {
    echo 'Erro : '.$mail->ErrorInfo;
}
    
16.06.2016 / 19:12