联系表格 - JQuery& PHP - 发送电子邮件不起作用

时间:2017-09-22 03:50:22

标签: javascript php jquery email

我的代码可以使用PHP发送和发送电子邮件吗?请看一下:

HTML:

<form class="contact_form" method="POST" action="mail.php">
    <div class="input">
        <label class="pl">Imię</label>
        <input id="name" type="text" name="name">
    </div>
    <div class="input">
        <label>E-mail</label>
        <input id="email" type="text" name="email">
    </div>
    <div class="input">
        <label class="pl">Temat</label>
        <input id="subject" type="text" name="subject">
    </div>
    <div class="message">
        <label class="pl">Twoja wiadomość</label>
        <textarea id="message" name="message"></textarea>
    </div>
    <div class="input">
        <input id="submit_pl" class="button pl" type="submit" value="Wyślij">
        <input id="reset_pl" class="button pl" type="reset" value="Resetuj">
    </div>
</form>

JS / JQuery的:

// Form Validation ------------------------------------ //
  // Form is not valid on page load //
  var isFormValid = false;

  // input variables //
  var name = $('#name');
  var email = $('#email');
  var subject = $('#subject');
  var message = $('#message');
  var subtmitBtnPl = $('#submit_pl');
  var subtmitBtnEn = $('#submit_eng');
  var resetBtnPl = $('#reset_pl');
  var resetBtnEn = $('#reset_eng');

  var form = $('.contact_form');

  var isNameValid = function () {
    var nameVal = name.val();
    return nameVal.length > 0 && nameVal.length <= 100;
  };

  var isEmailValid = function () {
    var emailVal = email.val();
    return emailVal.indexOf('@') > -1 && emailVal.length <= 100;
  };

  var isSubjectValid = function () {
    var subjectVal = subject.val();
    return subjectVal.length > 0 && subjectVal.length <= 100;
  };

  var isMessageValid = function () {
    var messageVal = message.val();
    return messageVal.length > 0 && messageVal.length <= 500;
  };

  function formValidation() {
    var isFormValid = isNameValid() && isEmailValid() && isSubjectValid() && isMessageValid();

    if (isFormValid === false) {
      alert('error');
    } else {
      alert('succes');
      form[0].reset();
    }
  };

  form.submit(function(event) {
    event.preventDefault();
    formValidation();
  });

PHP:

<?php
if (isset($_POST['name']) && isset($_POST['email']) && isset($_POST['subject'])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $subject = $_POST['subject'];
    $body = $_POST['message'];
    $to = 'xyz@gmail.com';

    //headers
    $headers = "From: ".$name." <".$email.">\r\n";
    $headers .= "Reply-To: ".$email."\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/html; charset-utf-8";

    //send
    $send = mail($to, $subject, $body, $headers);
    if ($send) {
        echo '<br>';
        echo 'Thanks for contacting me. I will be with You shorty.';
    } else {
        echo 'error';
    }
}
?>

我对PHP非常缺乏经验。你能告诉我为什么我的联系表格没有工作/发送电子邮件吗?

JQuery验证工作正常。 这个项目就在我的驱动器上而不是localhost(PHP mail()可以工作吗?)。 当然PHP文件的名称是“mail.php”。

1 个答案:

答案 0 :(得分:-1)

安装XAMPP,WAMPP等Web服务器