簡體   English   中英

從輸入表單獲取電子郵件

[英]Get e-mail from input form

試圖使它起作用。 我知道我需要啟動並在郵件服務器上運行才能使它正常工作,但是,我應該能夠發出錯誤消息。 因為當我直接在控制台中驗證“成功”和“錯誤”功能下的代碼時,它可以工作並顯示消息。 但是,當我按下“提交”按鈕時卻沒有。

--html--

   <div class="row">
        <form id="contactForm-1" name="sentMessage" novalidate>
           <div class="form-group">
             <input class="form-control" id="email-1" type="email" placeholder="Your Email *" required data-validation-required-message="Please enter your email address.">
             <p class="help-block text-danger"></p>
           </div>
        </form>
        <div class="clearfix"></div>
        <div class="col-lg-12 text-center">
        <div id="success-1"></div>
           <button id="sendMessageButton-1" class="btn btn-primary btn-xl text-uppercase" type="submit">Send Message</button>
        </div>

--JS--

 $(function() {

 $("#contactForm-1 input").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 email = $("input#email-1").val();
  // Check for white space in name for Success/Fail message
  $this = $("#sendMessageButton-1");
  $this.prop("disabled", true); // Disable submit button until AJAX call is complete to prevent duplicate messages
  $.ajax({
    url: "././mail/contact_me_start.php",
    type: "POST",
    data: {
      email: email
    },
    cache: false,
    success: function() {
      // Success message
      $('#success-1').html("<div class='alert alert-success'>");
      $('#success-1 > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
        .append("</button>");
      $('#success-1 > .alert-success')
        .append("<strong>Your message has been sent. </strong>");
      $('#success-1 > .alert-success')
        .append('</div>');
      //clear all fields
      $('#contactForm-1').trigger("reset");
    },
    error: function() {
      // Fail message
      $('#success-1').html("<div class='alert alert-danger'>");
      $('#success-1 > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
        .append("</button>");
      $('#success-1 > .alert-danger').append($("<strong>").text("Sorry, it seems that my mail server is not responding. Please try again later!"));
      $('#success-1 > .alert-danger').append('</div>');
      //clear all fields
      $('#contactForm-1').trigger("reset");
    },
    complete: function() {
      setTimeout(function() {
        $this.prop("disabled", false); // Re-enable submit button when AJAX call is complete
      }, 1000);
    }
  });
},
filter: function() {
  return $(this).is(":visible");
   },
  });

  $("a[data-toggle=\"tab\"]").click(function(e) {
    e.preventDefault();
    $(this).tab("show");
  });
 });

--PHP--

   <?php
   // Check for empty fields
   if(empty($_POST['email'])     ||
   !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
   {
   echo "No arguments Provided!";
   return false;
   }

$email_address = strip_tags(htmlspecialchars($_POST['email']));

// Create the email and send the message
$to = 'yourname@yourdomain.com'; // Add your email address inbetween the '' replacing yourname@yourdomain.com - This is where the form will send a message to.
$email_subject = "Website Contact Form: ";
$email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: \n\nEmail: $email_address\n\nPhone: \n\nMessage:";
$headers = "From: noreply@yourdomain.com"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
return true;
?>

您應該將“ submit按鈕放在表單中以使其正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM