My contact form returns errors when sending messages [closed]

0

I have a problem with my page, I'm sure it's something very simple but I'm not realizing where I have the error. The problem is that the contact form does not work, I get the following error:

"Sorry, there was an error in your data and the form can not be sent at this time." Detail of the errors. We are sorry but there seems to be a problem with the data sent. Please correct these errors and try again. " And I can not find a way to understand where the error is.

The link on my page is link

And here I attach my PHP code:

<?php
if(isset($_POST['email'])) {

     $email_to = "
 [email protected]";

    $email_subject = "Mail de la página";   

    function died($error) {

        // si hay algún error, el formulario puede desplegar su mensaje de aviso

        echo "Lo sentimos, hubo un error en sus datos y el formulario no puede ser enviado en este momento. ";

        echo "Detalle de los errores.<br /><br />";

        echo $error."<br /><br />";

        echo "Porfavor corrija estos errores e inténtelo de nuevo.<br /><br />";
        die();
    }

    // Se valida que los campos del formulairo estén llenos

    if(!isset($_POST['first_name']) ||

        !isset($_POST['last_name']) ||

        !isset($_POST['email']) ||

        !isset($_POST['telephone']) ||

        !isset($_POST['message'])) {

        died('Lo sentimos pero parece haber un problema con los datos enviados.');       

    }
 //En esta parte el valor "name" nos sirve para crear las variables que recolectaran la información de cada campo

    $first_name = $_POST['first_name']; // requerido

    $last_name = $_POST['last_name']; // requerido

    $email_from = $_POST['email']; // requerido

    $telephone = $_POST['telephone']; // no requerido 

    $message = $_POST['message']; // requerido

    $error = ""; //Linea numero 52

//En esta parte se verifica que la dirección de correo sea válida 

   $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

  if(!preg_match($email_exp,$email_from)) {

    $error_message .= 'La dirección de correo proporcionada no es válida.<br />';

  }

//En esta parte se validan las cadenas de texto

    $string_exp = "/^[A-Za-z .'-]+$/";

  if(!preg_match($string_exp,$first_name)) {

    $error_message .= 'El formato del nombre no es válido<br />';

  }

  if(!preg_match($string_exp,$last_name)) {

    $error_message .= 'el formato del apellido no es válido.<br />';

  }

  if(strlen($message) < 2) {

    $error_message .= 'El formato del texto no es válido.<br />';

  }

  if(strlen($error_message) > 0) {

    died($error_message);

  }

//A partir de aqui se contruye el cuerpo del mensaje tal y como llegará al correo

    $email_message = "Contenido del Mensaje.\n\n";



    function clean_string($string) {

      $bad = array("content-type","bcc:","to:","cc:","href");

      return str_replace($bad,"",$string);

    }



    $email_message .= "Nombre: ".clean_string($first_name)."\n";

    $email_message .= "Apellido: ".clean_string($last_name)."\n";

    $email_message .= "Email: ".clean_string($email_from)."\n";

    $email_message .= "Teléfono: ".clean_string($telephone)."\n";

    $email_message .= "Mensaje: ".clean_string($message)."\n";


//Se crean los encabezados del correo

$headers = 'From: '.$email_from."\r\n".

'Reply-To: '.$email_from."\r\n" .

'X-Mailer: PHP/' . phpversion();

@mail($email_to, $email_subject, $email_message, $headers);  

?>



<!-- incluye aqui tu propio mensaje de Éxito-->

Gracias! Nos pondremos en contacto contigo a la brevedad


<?php

}

?>
    
asked by Nicolas Dure 22.02.2016 в 15:48
source

1 answer

4

$error = "" //Linea numero 52
You are missing a semicolon at the end ..

It should be like this $error = ""; //Linea numero 52

You have updated the question so update my answer here below ...

First of all the form:

<form id="form" method="post" action="send_form_email.php" enctype="multipart/form-data">


                    <label>Nombre <span class="ind">*</span></label>
                        <input type="text" name="nombre" placeholder="Tu nombre" required>
                        <label>Apellido</label>
                        <input type="text" name="apellido" placeholder="Tu apellido" required>
                        <label>Email <span class="ind">*</span></label>
                        <input type="email" name="email" placeholder="[email protected]" required>
                        <label>Telefono <span class="ind">*</span></label>
                        <input type="text" placeholder="11-5045-0256" name="telefono">
                        <label for="mensaje">Mensaje:</label>
                        <textarea id= "mensaje" name="mensaje" placeholder= "Escribe tu consulta aquí, responderémos en breve." required=""></textarea>
                        <form method="post" enctype="multipart/form-data"><br>
                        <input type="file" size=60 name="file1"><br><br>
                        <input id= "submit" type= "submit" name= "submit" <a href="http://www.indgraphicdesign.com.ar/email_form.php" value= "Enviar" />
                        <br><br>
</form>

Notice that you have a form in a form .. =)

Second, in the form, the names of the inputs do not match what the post expects ..

Example:

<input type="text" name="apellido" placeholder="Tu apellido" required>

This would be sending the following:

$_POST['apellido'];

You have to match the variables that you have in this condition:

if(!isset($_POST['first_name']) ||

        !isset($_POST['last_name']) ||

        !isset($_POST['email']) ||

        !isset($_POST['telephone']) ||

        !isset($_POST['message'])) {

        died('Lo sentimos pero parece haber un problema con los datos enviados.');       

    }

The PHP isset function is set if it exists and that variable is defined, in this case for example

isset($_POST['first_name'])

What you are doing is validating if it exists and the $_POST['first_name'] is defined, if the variable is not defined it returns FALSE therefore the if it sends you to the "dead" function.

A tip in case you do not realize is to do the following .. In your PHP file to which you send the form above all do a print to $ _POST to see what is returning and add an exist to it only show that: P

print_r($_POST);

Greetings,

    
answered by 22.02.2016 / 16:01
source