AJAX form not displaying succes or error message

前端 未结 3 1524
忘了有多久
忘了有多久 2021-01-26 07:08

I\'ve asked this question before, but I still didn\'t figure it out. I\'ve made some changes, but I unfortunately still didn\'t get any luck. The form itself works, but it shoul

3条回答
  •  一个人的身影
    2021-01-26 07:16

    That just CAN'T be in one single file. You need two.

    Try like this... And edit your question with some new issue if any.

    And notice the submit event handler... Instead of a click handler.

    page.html:

    •  

    contact.php:

    if (isset($_POST['submit'])) {
    
    
      $email_to = "#";
    
      $email_subject = "#";
    
      $name = $_POST['name'];
      $lastname = $_POST['lastname'];
      $email = $_POST['email'];
      $phone = $_POST['phone'];
      $subject = $_POST['subject'];
      $information = $_POST['information'];
    
    
      $errorEmpty = false;
      $errorEmail = false;
    
      if (empty($name) || empty($email) || empty($subject) || empty($information)) {
        echo "Voer alle velden in!";
        $errorEmpty = true;
      }
      elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        echo "Geef een geldig E-mail!";
        $errorEmail = true;
      }
      else {
        $formcontent=" Naam: $name \n\n Achternaam: $lastname \n\n Email: $email \n\n Telefoon: $phone \n\n Onderwerp: $subject \n\n Informatie: $information";
        $mailheader = "From: ".$_POST["email"]."\r\n";
        $headers = "From: ". htmlspecialchars($_POST['name']) ." <" . $_POST['email'] . ">\r\n";
        $headers .= "Reply-To: " . $_POST['email'] . "\r\n";
        $headers .= "MIME-Version: 1.0\r\n";
        $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
        mail($email_to, $subject, $formcontent, $mailheader);
        echo "E-mail has been sent!";
      }
    
    }
    

提交回复
热议问题