0

I'm using a template website that is practically finished, but the form submit dont work. Pls anyone can help me?

Here is my basic html:

<form action="#" class="form-contact" id="contactForm">
  <div class="row">
    <div class="col-sm-6 col-md-6">
      <div class="form-group">
        <input type="text" class="form-control" id="p_name" placeholder="* Nome" required="">
        <div class="help-block with-errors"></div>
      </div>
    </div>
    <div class="col-sm-6 col-md-6">
      <div class="form-group">
        <input type="email" class="form-control" id="p_email" placeholder="* Email" required="">
        <div class="help-block with-errors"></div>
      </div>
    </div>
    <div class="col-sm-6 col-md-6">
      <div class="form-group">
        <input type="text" class="form-control" id="p_phone" placeholder="* Telefone">
        <div class="help-block with-errors"></div>
      </div>
    </div>
    <div class="col-sm-6 col-md-6">
      <div class="form-group">
        <input type="text" class="form-control" id="p_subject" placeholder="* Assunto">
        <div class="help-block with-errors"></div>
      </div>
    </div>
  </div>
  <div class="form-group">
    <textarea id="p_message" class="form-control" rows="6" placeholder="* Mensagem"></textarea>
    <div class="help-block with-errors"></div>
  </div>
  <div class="form-group">
    <div id="success"></div>
    <button type="submit" class="btn btn-primary">ENVIAR</button>
  </div>
</form>

here is my form-process.php:

<?php

$errorMSG = "";

// NAME
if (empty($_POST["name"])) {
    $errorMSG = "Name is required ";
} else {
    $name = $_POST["name"];
}

// EMAIL
if (empty($_POST["email"])) {
    $errorMSG .= "Email is required ";
} else {
    $email = $_POST["email"];
}

// SUBJECT
if (empty($_POST["subject"])) {
    $errorMSG .= "Subject is required ";
} else {
    $subject = $_POST["subject"];
}

// MESSAGE
if (empty($_POST["message"])) {
    $errorMSG .= "Message is required ";
} else {
    $message = $_POST["message"];
}


$EmailTo = "myemail@outlook.com";
$Subject = $subject;

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $message;
$Body .= "\n";

// send email
$success = mail($EmailTo, $Subject, $Body, "From:".$email);

// redirect to success page
if ($success && $errorMSG == ""){
   echo "success";
}else{
    if($errorMSG == ""){
        echo "Something went wrong :(";
    } else {
        echo $errorMSG;
    }
}

?>

Here is my ajax/javascript for sending the email:

$("#contactForm").validator().on("submit", function (event) {
    if (event.isDefaultPrevented()) {
        // handle the invalid form...
        formError();
        submitMSG(false, "Did you fill in the form properly?");
    } else {
        // everything looks good!
        event.preventDefault();
        submitForm();
    }
});


function submitForm(){
    // Initiate Variables With Form Content
    var name = $("#p_name").val();
    var email = $("#p_email").val();
    var subject = $("#p_subject").val();
    var message = $("#p_message").val();

    $.ajax({
        type: "POST",
        url: "php/form-process.php",
        data: "name=" + name + "&email=" + email + "&subject=" + subject + "&message=" + message,
        success : function(text){
            if (text == "success"){
                formSuccess();
            } else {
                formError();
                submitMSG(false,text);
            }
        }
    });
}

function formSuccess(){
    $("#contactForm")[0].reset();
    submitMSG(true, "Message Submitted!")
}

function formError(){
    $("#contactForm").removeClass().addClass('shake animated').one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function(){
        $(this).removeClass();
    });
}

function submitMSG(valid, msg){
    if(valid){
        var msgClasses = "h3 text-center tada animated text-success";
    } else {
        var msgClasses = "h3 text-center text-danger";
    }
    $("#success").removeClass().addClass(msgClasses).text(msg);
}

I was never able to send a form, I always get the error "Did you fill in the form properly?"

  • All of your form elements need 'name' attributes, e.g. `name="name" name="email"` etc. – Jay Blanchard Sep 30 '20 at 18:58
  • @JayBlanchard sorry but dont understand – hpscorpion Sep 30 '20 at 19:03
  • For example `` – Jay Blanchard Sep 30 '20 at 19:22
  • @JayBlanchard Already change this right now, still dont work. I am desperate. I've been trying to fix this all day. Can I speak to you in private? my email is: tumblrmoans@outlook.com – hpscorpion Sep 30 '20 at 19:31
  • @Alexey Nazarov dont understand your edit – hpscorpion Sep 30 '20 at 19:57
  • Have you opened the developer tools in your browser to see if there is any error? Have you watched the AJAX request/response? – Jay Blanchard Oct 01 '20 at 12:29
  • @JayBlanchard friend i find the problem. Its my HOST. i just try on other host and work perfect. Code its ok. my isp host said dont use PHP mail().. and advised me to change to framework SMTP, like PHPMailer() or SwiftMailer(). No ideia how to change this. can you help me? – hpscorpion Oct 01 '20 at 12:53
  • I'd use PHPMailer. It is not an easy change though, and not something we can do here on Stack Overflow. – Jay Blanchard Oct 01 '20 at 13:17
  • @JayBlanchard contact me by email please. i have a code maybe can work with phpmailer if you can check pls. – hpscorpion Oct 01 '20 at 13:38
  • This is not how this works. I am not available for private tutoring, but there are sites out there who offer these services....for a price. – Jay Blanchard Oct 01 '20 at 14:14
  • @JayBlanchard I understand. I just made a new question with my phpmailer code. pls check if possible. https://stackoverflow.com/questions/64157332/change-phpmail-code-to-phpmailer-i-think-its-close-but-still-dont-work – hpscorpion Oct 01 '20 at 14:22

0 Answers0