基础联系表单与遵守-php不起作用



我已经尽了一切努力让这个表单正常工作,但没有成功。

我想遵守现在正在工作,但我的php没有发送电子邮件。我甚至不认为我的php会被调用。

我的代码低于

表单代码434线

<form id="myForm" data-abide action="mail.php" method="post">
                                <div class="contactform">
                                    <div class="item item-pair">
                                      <label for="name">Full Name
                                        <input type="text" name="name" id="name" class="small-input cat_textbox" required   pattern="[a-zA-Z]+"  maxlength="255">
                                         <small class="error small-input">Name is required and must be a string.</small>
                                      </label>
                                      <label for="email">Email Address
                                        <input type="text" name="email" id="email" class="small-input cat_textbox" maxlength="255" required  >
                                         <small class="error small-input">An email address is required.</small>
                                      </label>
                                    </div>
                                    <div class="item">
                                      <label>Comments</label>
                                      <textarea  cols="10" name="message" id="message" rows="4" class="cat_listbox" required ></textarea>
                                      <small class="error">Please enter your comments</small>
                                    </div>
                                    <div class="item">
                                      <input class="button alert small" type="submit" value="Submit" id="catwebformbutton">
                                    </div>
                                </div>
                              </form>

javascript代码627线

<script>
         $('#myForm').submit(function(e) {
            //prevent default form submitting.
            e.preventDefault();
            $(this).on('valid', function() {
              var name = $("input#name").val();
              var email = $("input#email").val();
              var message = $("textarea#message").val();
              //Data for reponse
              var dataString = 'name=' + name +
                '&email=' + email +
                '&message=' + message;
              //Begin Ajax call
              $.ajax({
                type: "POST",
                url:"mail.php",
                data: dataString,
                success: function(data){
                  $('.contactform').html("<div id='thanks'></div>");
                    $('#thanks').html("<h2>Thanks!</h2>")
                    .append("<p>Dear "+ name +", I will get back to you as soon as I can ;)</p>")
                    .hide()
                    .fadeIn(1500);
                },
              }); //ajax call
              return false;
            });
          });    

        </script>

html链接http://tayrani.com

请帮助

<?php
$name = $_POST["name"];
$email = $_POST["email"];
$comments = $_POST["message"];
$msg = "
Name:$name
Email:$email
Comment:
$comments";
$to = "tayrani@hotmail.com";
$subject = "website email";
$message = $msg;
$headers = "form";
mail($to,$subject,$message,$headers);
?>

感谢您的帮助,让它工作起来。事实证明,Hotmail出于某种原因不接受电子邮件。所以,我用Gmail帐户替换了Hotmail帐户,它起了作用。我还用以下更新了我的代码

表单的html代码

<form id="myForm" data-abide="ajax" action="mail.php" method="post">
    <div class="contactform">
        <div class="item item-pair">
          <label for="name">Full Name
            <input type="text" name="name" id="name" class="small-input cat_textbox" required   pattern="[a-zA-Z]+"  maxlength="255">
             <small class="error small-input">Name is required and must be a string.</small>
          </label>
          <label for="email">Email Address
            <input type="email" name="email" id="email" class="small-input cat_textbox" maxlength="255" required  >
             <small class="error small-input">An email address is required.</small>
          </label>
        </div>
        <div class="item">
          <label>Comments</label>
          <textarea  cols="10" name="message" id="message" rows="4" class="cat_listbox" required ></textarea>
          <small class="error">Please enter your comments</small>
        </div>
        <div class="item">
          <input class="button alert small" type="submit" value="Submit" id="catwebformbutton" name="btnSubmit">
        </div>
    </div>
</form>

我的javascript代码包括修复提交两次问题

<script>
 $('#myForm').submit(function(e) {
    //prevent default form submitting so it can run the ajax code first 
    e.preventDefault();
    $(this).on('valid', function() {    //if the form is valid then grab the values of these IDs (name, email, message) 
      var name = $("input#name").val();
      var email = $("input#email").val();
      var message = $("textarea#message").val();
      //Data for reponse (store the values here)
      var dataString = 'name=' + name +
        '&email=' + email +
        '&message=' + message;
      //Begin Ajax call
      $.ajax({
        type: "POST",
        url:"mail.php", //runs the php code 
        data: dataString, //stores the data to be passed 
        success: function(data){ // if success then generate the div and append the the following
          $('.contactform').html("<div id='thanks'></div>");
            $('#thanks').html("<br /><h4>Thanks!</h4>")
            .append('<p><span style="font-size:1.5em;">Hey</span> <span class="fancy">'+ name +'</span>,<br />I&acute;ll get back to you as soon as I can ;)</p>')
            .hide()
            .fadeIn(1500);
        },
        error: function(jqXHR, status, error){ //this is to check if there is any error
            alert("status: " + status + " message: " + error);
        }
      }); //End Ajax call 
      //return false;
    });
  });    
</script>
<script>
    $(document).foundation('abide', 'events'); // this was originally before the above code, but that makes the javascript code runs twice before submitting. Moved after and that fixes it.
</script>

这是php代码

<?php
if(isset($_POST["name"])){
    $name = $_POST["name"];
    $email = $_POST["email"];
    $comments = $_POST["message"];
    $msg = "
    Name: $name
    Email: $email
    Comments:
    $comments";
    $to = "h2hussein@gmail.com";
    $subject = "Tayrani.com Contact Form";
    $headers = "From: <$email>";
    mail($to,$subject,$msg,$headers);
}else{
}
?>

我挣扎了3天才完成这项工作,但多亏了我的同事/朋友Adam,他真的帮了我。

我希望这对其他人有用。

谢谢,

侯赛因tayrani.com

最新更新