PHP - no muestra mensaje de éxito al enviar formulario PHP

 
Vista:

no muestra mensaje de éxito al enviar formulario PHP

Publicado por Eva (1 intervención) el 03/11/2020 17:53:56
Buenas a todos, hace poco que he empezado a tocar php y javascript, y estoy creando una página y me estoy peleando con el código del formulario. Bien, cuando relleno el formulario y pulso en enviar el mensaje me llega al correo, el problema es que en la web no se muestra ningún mensaje de éxito y no tengo muy claro como arreglarlo o como implementarlo en mi código. Tengo un código php para el formulario al que llamo en el html <form action='contacto_process.php'> y un código javascript que valida los datos ingresados por el usuario (numero de caracteres, correo.. etc)

Resumen: cuando pulso en enviar el correo llega pero en la web no se muestra ningun mensaje de exito.

(he censurado los correos en los codigos)

CÓDIGO PHP:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
 
    $to = "micorreo@xxxx.com";
    $from = $_REQUEST['email'];
    $name = $_REQUEST['name'];
    $subject = $_REQUEST['subject'];
    $number = $_REQUEST['number'];
    $cmessage = $_REQUEST['message'];
 
    $headers = "From: $from";
	$headers = "From: " . $from . "\r\n";
	$headers .= "Reply-To: ". $from . "\r\n";
	$headers .= "MIME-Version: 1.0\r\n";
	$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
 
    $subject = "Tienes un nuevo mensaje de tu web.";
 
    $logo = 'RG';
    $link = 'www.xxx.com';
 
	$body = "<!DOCTYPE html><html lang='en'><head><meta charset='UTF-8'><title>Express Mail</title></head><body>";
	$body .= "<table style='width: 100%;'>";
	$body .= "<thead style='text-align: center;'><tr><td style='border:none;' colspan='2'>";
	$body .= "<a href='{$link}'><img src='{$logo}' alt=''></a><br><br>";
	$body .= "</td></tr></thead><tbody><tr>";
	$body .= "<td style='border:none;'><strong>Name:</strong> {$name}</td>";
	$body .= "<td style='border:none;'><strong>Email:</strong> {$from}</td>";
	$body .= "</tr>";
	$body .= "<tr><td style='border:none;'><strong>Subject:</strong> {$csubject}</td></tr>";
	$body .= "<tr><td></td></tr>";
	$body .= "<tr><td colspan='2' style='border:none;'>{$cmessage}</td></tr>";
	$body .= "</tbody></table>";
	$body .= "</body></html>";
 
	$send = mail($to, $subject, $body, $headers);
 
?>
 
<?php
if(isset($_POST['email'])) {
 
// Debes editar las próximas dos líneas de código de acuerdo con tus preferencias</bold>
$email_to = "xxx@xxx.com";
$email_subject = "Contacto desde el sitio web";
 
// Aquí se deberían validar los datos ingresados por el usuario</bold>
if(!isset($_POST['email']) ||
!isset($_POST['name']) ||
!isset($_POST['subjet']) ||
!isset($_POST['message'])) {
 
echo "<b>Ocurrió un error y el formulario no ha sido enviado. </b><br />";
echo "Por favor, vuelva atrás y verifique la información ingresada<br />";
die();
}
 
$email_message = "Detalles del formulario de contacto:\n\n";
$email_message .= "Nombre: " . $_POST['name'] . "\n";
$email_message .= "E-mail: " . $_POST['email'] . "\n";
$email_message .= "Mensaje: " . $_POST['message'] . "\n";
$email_message .= "Asunto: " . $_POST['subjet'] . "\n\n";
 
 
<bold>// Ahora se envía el e-mail usando la función mail() de PHP</bold>
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
 
echo "¡El formulario se ha enviado con éxito!";
}
?>

CODIGO JS:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
$(document).ready(function () {
  (function ($) {
    "use strict";
 
    jQuery.validator.addMethod(
      "answercheck",
      function (value, element) {
        return this.optional(element) || /^\bcat\b$/.test(value);
      },
      "type the correct answer -_-"
    );
 
    // validate contactForm form
    $(function () {
      $("#contactForm").validate({
        rules: {
          name: {
            required: true,
            minlength: 2,
          },
          subject: {
            required: true,
            minlength: 4,
          },
          number: {
            required: true,
            minlength: 5,
          },
          email: {
            required: true,
            email: true,
          },
          message: {
            required: true,
            minlength: 20,
          },
        },
        messages: {
          name: {
            required: "come on, you have a name, don't you?",
            minlength: "your name must consist of at least 2 characters",
          },
          subject: {
            required: "come on, you have a subject, don't you?",
            minlength: "your subject must consist of at least 4 characters",
          },
          number: {
            required: "come on, you have a number, don't you?",
            minlength: "your Number must consist of at least 5 characters",
          },
          email: {
            required: "no email, no message",
          },
          message: {
            required:
              "um...yea, you have to write something to send this form.",
            minlength: "thats all? really?",
          },
        },
        submitHandler: function (form) {
          $(form).ajaxSubmit({
            type: "POST",
            data: $(form).serialize(),
            url: "contact_process.php",
            success: function () {
              $("#contactForm :input").attr("disabled", "disabled");
              $("#contactForm").fadeTo("slow", 1, function () {
                $(this).find(":input").attr("disabled", "disabled");
                $(this).find("label").css("cursor", "default");
                $("#success").fadeIn();
                $(".modal").modal("hide");
                $("#success").modal("show");
              });
            },
            error: function () {
              $("#contactForm").fadeTo("slow", 1, function () {
                $("#error").fadeIn();
                $(".modal").modal("hide");
                $("#error").modal("show");
              });
            },
          });
        },
      });
    });
  })(jQuery);
});
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Yoel
Val: 617
Bronce
Ha mantenido su posición en PHP (en relación al último mes)
Gráfica de PHP

no muestra mensaje de éxito al enviar formulario PHP

Publicado por Yoel (198 intervenciones) el 04/11/2020 00:10:08
Hola Eva, según puedo ver en tu código no estas capturando el valor que estas imprimiendo en el php. A continuación, te dejo un ejemplo de de como lo puedes hacer, es solo un fragmento de tu código, tendrías que modificar el original.

Gracias

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
success: function (data) { //Colocamos la variable data para capturar el valor impreso en php
      alert(data); //Mostramos el valor de la variable data con el resultado impreso en el php
 
      $("#contactForm :input").attr("disabled", "disabled");
      $("#contactForm").fadeTo("slow", 1, function () {
        $(this).find(":input").attr("disabled", "disabled");
        $(this).find("label").css("cursor", "default");
        $("#success").fadeIn();
        $(".modal").modal("hide");
        $("#success").modal("show");
      });
    },
    error: function (data) { //Colocamos la variable data para capturar el valor impreso en php
      alert(data); //Mostramos el valor de la variable data con el resultado impreso en el php
 
      $("#contactForm").fadeTo("slow", 1, function () {
        $("#error").fadeIn();
        $(".modal").modal("hide");
        $("#error").modal("show");
      });
    },
  });
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar