Submit php does not work

Asked

Viewed 946 times

1

My contact form is not sending, someone could see what mistake I’m making?

HTML:

 <form id="contactForm" novalidate class="s-form wow zoomInUp" data-wow-delay="0.5s">
    <div class="s-relative">
        <select name="user-topic" id="user-topic" class="m-select">
                <option value="Not select">O que você precisa?</option>
                <option value="Topic 1">Solicitar contato de um vendedor</option>
                <option value="Topic 2">Agendar Revisão</option>
                <option value="Topic 3">Cotação de Peças</option>
                   <option value="Topic 3">Crítica/Sugestão</option>
            </select>
        <span class="fa fa-caret-down"></span>
    </div>
    <input type="text" placeholder="NOME" value="" name="user-name" id="user-name" />
    <input type="text" placeholder="E-MAIL" value="" name="user-email" id="user-email" />
    <input type="text" placeholder="TELEFONE" value="" name="user-phone" id="user-phone" data-mask="(00) 0000-0000" />
    <textarea id="user-message" name="user-message" placeholder="DEIXE SUA MENSAGEM"></textarea>
    <button type="submit" class="btn m-btn">Enviar mensagem<span class="fa fa-angle-right"></span></button>
 </form>

PHP:

  <?php
  // Check for empty fields
  if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['message']) || !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL)){
    echo json_encode(array('error'=>'true'));
    return false;
  }

  $name = $_POST['name'];
  $email_address = $_POST['email'];
  $message = $_POST['message'];
  $lastname = $_POST['lastname'];
  $phone = $_POST['phone'];
  $subject = ($_POST['subject'] ? $_POST['subject'] : "Website Contact Form:  $name");



  // Create the email and send the message
  $to = '[email protected]';// Add your email address inbetween the '' replacing [email protected] - This is where the form will send a message to.
  $email_subject = $subject;
  $email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nLast Name: $lastname\n\nEmail: $email_address\n\nPhone: $phone\n\nMessage:\n$message";
  $headers = "From: [email protected]\n"; // This is the email address the generated message will be from. We recommend using something like [email protected].
  $headers .= "Reply-To: $email_address";   
  mail($to,$email_subject,$email_body,$headers);
  echo json_encode(array('success'=>'true'));
  return true;          
  ?>

JS:

$(function() {
$("input,textarea").jqBootstrapValidation({
    preventSubmit: true,
    submitError: function($form, event, errors) {
        // additional error messages or events
    },
    submitSuccess: function($form, event) {
        event.preventDefault(); // prevent default submit behaviour
        // get values from FORM
        var topic = $("select#user-topic").val();
        var name = $("input#user-name").val();
        var email = $("input#user-email").val();
        var phone = $("input#user-phone").val();
        var message = $("textarea#user-message").val();
        var firstName = name; // For Success/Failure Message
        // Check for white space in name for Success/Fail message
        if (firstName.indexOf(' ') >= 0) {
            firstName = name.split(' ').slice(0, -1).join(' ');
        }
        $.ajax({
            url: "././mail/contact_me.php",
            type: "POST",
            dataType: 'json',
            data: {
                topic: topic,
                name: name,
                email: email,
                phone: phone,
                message: message
            },
            cache: false,
            success: function(data) {
                if(data.error){
                    // Fail message
                    $('#success').html("<div class='alert alert-danger'>");
                    $('#success > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;").append("</button>");
                    $('#success > .alert-danger').append("<span>Sorry " + firstName + ", it seems that my mail server is not responding. Please try again!</span>");
                    $('#success > .alert-danger').append('</div>');
                    //clear all fields
                    $('#contactForm').trigger("reset");
                }
                else if(data.success){
                    // Success message
                    $('#success').html("<div class='alert alert-success'>");
                    $('#success > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;").append("</button>");
                    $('#success > .alert-success').append("<span>Your message has been sent. </span>");
                    $('#success > .alert-success').append('</div>');
                    //clear all fields
                    $('#contactForm').trigger("reset");
                }
            }
        })
    },
    filter: function() {
        return $(this).is(":visible");
    },
});
});


/*When clicking on Full hide fail/success boxes */
$('#name').focus(function() {
    $('#success').html('');
});

The page is this here.

Could be something from the site be in a subdomain?

  • looks like your form is sending via GET, puts a method="post" in the form tag to see if it works.

  • put method="post", but unfortunately it did not work

1 answer

2


Good afternoon Pedro, Good come on...

HTML:

<form id="contactForm" name="form1" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" class="s-form wow zoomInUp" data-wow-delay="0.5s">
<div class="s-relative">
    <select name="user-topic" id="user-topic" class="m-select">
            <option value="Not select">O que você precisa?</option>
            <option value="Topic 1">Solicitar contato de um vendedor</option>
            <option value="Topic 2">Agendar Revisão</option>
            <option value="Topic 3">Cotação de Peças</option>
               <option value="Topic 3">Crítica/Sugestão</option>
        </select>
    <span class="fa fa-caret-down"></span>
</div>
<input type="text" placeholder="NOME" value="" name="user-name" id="user-name" />
<input type="text" placeholder="E-MAIL" value="" name="user-email" id="user-email" />
<input type="text" placeholder="TELEFONE" value="" name="user-phone" id="user-phone" data-mask="(00) 0000-0000" />
<textarea id="user-message" name="user-message" placeholder="DEIXE SUA MENSAGEM"></textarea>
<button type="submit" name="enviar" class="btn m-btn">Enviar mensagem<span class="fa fa-angle-right"></span></button>

PHP:

  <?php
  // Check for empty fields
  if(isset($_POST['enviar'])){
  if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['message']) || !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL)){
    echo json_encode(array('error'=>'true'));
    return false;
  }

  $name = $_POST['name'];
  $email_address = $_POST['email'];
  $message = $_POST['message'];
  $lastname = $_POST['lastname'];
  $phone = $_POST['phone'];
  $subject = ($_POST['subject'] ? $_POST['subject'] : "Website Contact Form:  $name");



  // Create the email and send the message
  $to = '[email protected]';// Add your email address inbetween the '' replacing [email protected] - This is where the form will send a message to.
  $email_subject = $subject;
  $email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nLast Name: $lastname\n\nEmail: $email_address\n\nPhone: $phone\n\nMessage:\n$message";
  $headers = "From: [email protected]\n"; // This is the email address the generated message will be from. We recommend using something like [email protected].
  $headers .= "Reply-To: $email_address";   
  mail($to,$email_subject,$email_body,$headers);
  echo json_encode(array('success'=>'true'));
  return true;  }


  ?>

JS:

$(function() {
$("input,textarea").jqBootstrapValidation({
    preventSubmit: true,
    submitError: function($form, event, errors) {
        // additional error messages or events
    },
    submitSuccess: function($form, event) {
        event.preventDefault(); // prevent default submit behaviour
        // get values from FORM
        var topic = $("select#user-topic").val();
        var name = $("input#user-name").val();
        var email = $("input#user-email").val();
        var phone = $("input#user-phone").val();
        var message = $("textarea#user-message").val();
        var firstName = name; // For Success/Failure Message
        // Check for white space in name for Success/Fail message
        if (firstName.indexOf(' ') >= 0) {
            firstName = name.split(' ').slice(0, -1).join(' ');
        }
        $.ajax({
            url: "././mail/contact_me.php",
            type: "POST",
            dataType: 'json',
            data: {
                topic: topic,
                name: name,
                email: email,
                phone: phone,
                message: message
            },
            cache: false,
            success: function(data) {
                if(data.error){
                    // Fail message
                    $('#success').html("<div class='alert alert-danger'>");
                    $('#success > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;").append("</button>");
                    $('#success > .alert-danger').append("<span>Sorry " + firstName + ", it seems that my mail server is not responding. Please try again!</span>");
                    $('#success > .alert-danger').append('</div>');
                    //clear all fields
                    $('#contactForm').trigger("reset");
                }
                else if(data.success){
                    // Success message
                    $('#success').html("<div class='alert alert-success'>");
                    $('#success > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;").append("</button>");
                    $('#success > .alert-success').append("<span>Your message has been sent. </span>");
                    $('#success > .alert-success').append('</div>');
                    //clear all fields
                    $('#contactForm').trigger("reset");
                }
            }
        })
    },
    filter: function() {
        return $(this).is(":visible");
    },
});
});


/*When clicking on Full hide fail/success boxes */
$('#name').focus(function() {
    $('#success').html('');
});

Tested and running ! Abs

  • I made the changes, unfortunately it’s not working yet.

  • @pedro is all on the same page ? o PHP, HTML and JAVASCRIPT ?

  • no, ta so, the HTML is in the /site, the PHP is in the /site/mail, and the Javascript is in the /site/Assets/contact. it would be better to put everything together?

  • @Pedrohenriquekuzminskas adds me in skype I have a code here : ciclum.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.