How can I clean the fields of my form?

1

I am using PHPMailer, when sending the data to the GMail email but after clicking "accept" in the alert verifying that the email was sent, my fields are still full of the information I sent, does anyone know how I can reset them? ?

This is my code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<title>Formulario</title> <!-- Aquí va el título de la página -->

</head>

<body>
<?php

    $Nombre = $_POST['Nombre'];
    $Email = $_POST['Email'];
    $Mensaje = $_POST['Mensaje'];
    $Telefono = $_POST['Telefono'];    

    if ($Nombre=='' || $Email=='' || $Mensaje=='' || $Telefono==''){

        echo "<script>alert('Los campos marcados con * son obligatorios');location.href ='javascript:history.back()';</script>";

    }else{


        require("archivosformulario/class.phpmailer.php");
        $mail = new PHPMailer();    
        $mail->From     = $Email;
        $mail->FromName = $Nombre; 
        $mail->AddAddress("[email protected]"); // Dirección a la que llegaran los mensajes.

        // Aquí van los datos que apareceran en el correo que reciba
        //adjuntamos un archivo 

        $mail->WordWrap = 50; 
        $mail->IsHTML(true);     
        $mail->Subject  =  "Comentarios Tiendas El Golazo";
        $mail->Body     =  "Nombre: $Nombre \n<br />".    
        "Email: $Email \n<br />".    
        "Mensaje: $Mensaje \n<br />".
        "Telefono: $Telefono \n<br />";       

        // Datos del servidor SMTP

        $mail->IsSMTP(); 
        $mail->Host = "ssl://smtp.gmail.com:465";  // Servidor de Salida.
        $mail->SMTPAuth = true; 
        $mail->Username = "[email protected]";  // Correo Electrónico
        $mail->Password = ""; // Contraseña

        if ($mail->Send())
        echo "<script>alert('Formulario enviado exitosamente, le responderemos lo más pronto posible.');location.href ='javascript:history.back()';</script>";    

        else
        echo "<script>alert('Error al enviar el formulario');location.href ='javascript:history.back()';</script>";

    }
    ?>
</body>
</html>

This is displayed when sending the information and I want to know how to clean it after alert :

Any suggestions?

    
asked by Alejandro Ruiz 16.05.2016 в 03:10
source

2 answers

0

The problem is here:

if ($mail->Send())
echo "<script>alert('Formulario enviado exitosamente, le responderemos lo más pronto posible.');location.href ='javascript:history.back()';</script>";    

else
echo "<script>alert('Error al enviar el formulario');location.href ='javascript:history.back()';</script>";

And specifically in this part of here: location.href ='javascript:history.back() (which by the way you could simplify so that it was only history.back() ), because what you're doing is telling the browser to go one step back in the history (to the page of the form). Then the browser will go back one step in the history but will generally keep the information entered by the user (which is what you want to avoid).

You could solve this in different ways:

  • Instead of doing history.back() , use the location.href to redirect to the page that contains the form directly (eg: location.href='pagina_formulario.php' ). This method will cause that instead of loading the page that is in the cache, a new one will be generated with the blank form.

  • Use the pageshow event in the window to clean the form. This event is executed whenever the history is traversed (when loading a new page or pressing the forward / back buttons).

    The JavaScript code would be very simple and would go on the form page (after the form has been loaded):

    <script>
    // cuando se muestre la página
    window.addEventListener('pageshow', function(event) {
        // borra el formulario (asumiendo que sólo hay uno; si hay más, especifica su Id)
        document.querySelector("form").reset();
    });
    </script>
    
answered by 16.05.2016 / 14:43
source
1

Through javascript the reset () method, to reset the form, for example:

document.getElementById("myForm").reset();
    
answered by 16.05.2016 в 04:10