Contact form not valid and does not send some fields

Posted on

Question :

Hello, this is my first question here! The problem is this …
I took a form on the net and tried to adapt it to my needs, but the fields I created ( $phone and $empresa ) do not want to validate, I had to remove the validation of these fields, the form sent but these fields arrive blank I am mto noob at all, mostly php: ‘(

This is the form with bugged validation x_x



<form id="contactform" action="chamadotecnico.php" name="contactform" method="post">
  <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
    <input type="text" name="name" id="name" class="form-control" placeholder="Nome">
    <input type="text" name="phone" id="phone" class="form-control" placeholder="Telefone">
  <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
    <input type="text" name="email" id="email" class="form-control" placeholder="E-mail">
    <input type="text" name="empresa" id="empresa" class="form-control" placeholder="Empresa">
  <div class="col-lg-12">
    <textarea class="form-control" name="comments" id="comments" rows="6" placeholder="Descrição do problema técnico"></textarea>
    <button type="submit" value="SEND" id="submit" class="btn btn-lg btn-primary pull-right">Enviar</button>



if(!$_POST) exit;

// Verificação de endereço de email, não mexa!
function isEmail($email) {

if (!defined("PHP_EOL")) define("PHP_EOL", "rn");

$name     = $_POST['name'];
$email    = $_POST['email'];
$phone    = $_POST['phone'];
$empresa  = $_POST['empresa'];
$comments = $_POST['comments'];

if(trim($name) == '') {
    echo '<div class="error_message">Informe o seu nome.</div>';
} else if(trim($email) == '') {
    echo '<div class="error_message">Insira um endereço de e-mail.</div>';
} else if(!isEmail($email)) {
    echo '<div class="error_message">Atenção! Você tem digitar um endereço de e-mail inválido, tente novamente.</div>';

if(trim($phone) == '') {
    echo '<div class="error_message">Informe um telefone para contato.</div>';
} else if(trim($empresa) == '') {
    echo '<div class="error_message">Informe o nome da sua empresa.</div>';
} else if(trim($comments) == '') {
    echo '<div class="error_message">Informe a descrição do seu problema.</div>';

if(get_magic_quotes_gpc()) {
    $comments = stripslashes($comments);

$address = "";
$e_subject = 'Chamado Técnico';

$e_body = "Nome: $namernEmail: $emailrnTelefone: $phonernEmpresa: $empresa" . PHP_EOL . PHP_EOL;
$e_content = "Descrição do problema:rn$comments" . PHP_EOL . PHP_EOL;
$e_reply = "Você pode entrar em contato $name via email, $email ou pelo telefone $phone";

$msg = wordwrap( $e_body . $e_content . $e_reply, 70 );

$headers = "From: $email" . PHP_EOL;
$headers .= "Reply-To: $email" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;

if(mail($address, $e_subject, $msg, $headers)) {

    echo "<fieldset>";
    echo "<div id='success_page'>";
    echo "<h1>E-mail enviada com sucesso!</h1>";
    echo "<p>Obrigado <strong>$name</strong>, sua mensagem foi enviada, em breve entraremos em contato.</p>";
    echo "</div>";
    echo "</fieldset>";

} else {

    echo 'ERROR!';


[edit] JS I found after friend’s help Sanction



        var action = $(this).attr('action');

        $("#message").slideUp(750,function() {

            .after('<img src="images/ajax-loader.gif" class="loader1" />')

        $.post(action, {
            name: $('#name').val(),
            email: $('#email').val(),
            website: $('#website').val(),
            subject: $('#subject').val(),
            comments: $('#comments').val(),
            verify: $('#verify').val()
                document.getElementById('message').innerHTML = data;
                $('#contactform img.loader').fadeOut('slow',function(){$(this).remove()});
                if(data.match('success') != null) $('#contactform').slideUp('slow');



        return false;




Answer :

The PHP code looks correct. Everything indicates that your form is sent by AJAX because:

  • there is a spinner at the bottom of the image
  • The error message is generated by echo followed by exit and should be included by javascript.



YoucanreplacethepassedJSONobjectasthesecondparameterof$.post()(below)with$(this).serialize(),thusallformfieldswillbesenttoPHP.Formoreinformationvisit .serialize ()

    name: $('#name').val(),
    email: $('#email').val(),
    website: $('#website').val(),
    subject: $('#subject').val(),
    comments: $('#comments').val(),
    verify: $('#verify').val()


Try to use strip_tags to remove any possible tag HTML that might have come jundo:

$phone = (trim(strip_tags($_POST['phone'])));


Leave a Reply

Your email address will not be published. Required fields are marked *