-2

I am trying to add a telephone field into a contact form but when I do, the script stops working and I don´t receive an email.

    <?php
// check if fields passed are empty
if(empty($_POST['name'])        ||
   empty($_POST['email'])       ||
   empty($_POST['telephone'])       ||
   empty($_POST['message']) ||
   !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
   {
    echo "No se ha introducido toda la información.";
    return false;
   }

$name = $_POST['name'];
$email_from = $_POST['email'];
$telephone = $_POST['telephone'];
$message = $_POST['message'];

// create email body and send it    
$to = 'h.........@gmail.com'; // put your email
$email_subject = "Formulario de Contacto Con la Mochila al Hombro";
$email_body =  "Has recibido un mensaje desde el formulario de contacto de la Web. \n\n".
               "Aquí están los detalles:\n\n".
               "Nombre: $name \n\n".
               "Email: $email_from\n\n".
               "Teléfono: $telephone\n\n".
               "Mensaje: $message";    
$headers = 'From: '.$email_from."\r\n".
            'X-Mailer: PHP/' . phpversion();
mail($to,$email_subject,$email_body,$headers);
return true;            
?>

And this is the html I am using:

        <div class="control-group">
            <div class="controls">
                <input type="phone" class="form-control" placeholder="Teléfono" id="telephone" required data-validation-required-message="Por favor, dinos tu teléfono para que podamos contactar contigo." />
            </div>
        </div>  

The form works perfectly fine if I don´t put the telephone in the php script, but as soon as I add it, it stops working. I am assuming I am setting some values wrong but I really don´t know much abut PHP.

I believe now that it is a problem with the AJAX validation script.You can see the script live in here http://talleresnaj2.com/js/contacto.min.js

2 Answers2

1

Your <input /> doesn't have a name attribute, hence its value is never sent to the PHP script, making empty($_POST['telephone']) return true.

You just need to add name="telephone" in your input.

roberto06
  • 3,844
  • 1
  • 18
  • 29
0

You forgot to add the input name.

Try

<input name="telephone" type="phone" class="form-control" placeholder="Teléfono" id="telephone" required data-validation-required-message="Por favor, dinos tu teléfono para que podamos contactar contigo." />
Axel
  • 1
  • 2