无法使用Ajax

时间:2019-02-11 19:42:48

标签: php jquery ajax

尝试通过电子邮件从表单发送结果。

我有一个请求用户信息的表格,例如姓名,电子邮件等。我能够使用js检索此信息。我遇到的问题是当我使用AJAX将值传递给PHP时,没有电子邮件发送出去。我检查了,没有错误出现。我在php中添加代码以查看未传递的内容,当我这样做时,没有任何值被传递至php文件。

以下代码是表格:

    <form name="sentMessage" id="contactForm">
              <div class="row">
                <div class="col-md-6">
                  <div class="form-group">
                    <input type="text" id="name" class="form-control" placeholder="Name" required="required">
                    <p class="help-block text-danger"></p>
                  </div>
                </div>
                <div class="col-md-6">
                  <div class="form-group">
                    <input type="email" id="email" class="form-control" placeholder="Email" required="required">
                    <p class="help-block text-danger"></p>
                  </div>
                </div>
              </div>
              <div class="form-group">
                <textarea id="message" class="form-control" rows="4" placeholder="Message" required="required"></textarea>
                <p class="help-block text-danger"></p>
              </div>
              <div id="success"></div>
              <button type="submit" class="btn btn-custom btn-lg">Send Message</button>
            </form>

以下是ajax调用:

submitSuccess: function($form, event) {
   event.preventDefault(); // prevent default submit behaviour
   // get values from FORM
   var name = $("input#name").val();
   var email = $("input#email").val();
   var message = $("textarea#message").val();
   var firstName = name; // For Success/Failure Message
   // Check for white space in name for Success/Fail message
   if (firstName.indexOf(' ') >= 0) {
           firstName = name.split(' ').slice(0, -1).join(' ');
    }
    $.ajax({
        url: "./mail/contact_me.php",
        type: "POST",
       data: {
            name: name,
            email: email,
            message: message
        },
        cache: false,
        success: function() {
           // Success message
           $('#success').html("<div class='alert alert-success'>");
           $('#success > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
           .append("</button>");
          $('#success > .alert-success')
            .append("<strong>Your message has been sent. </strong>");
          $('#success > .alert-success')
              .append('</div>');

             //clear all fields
           $('#contactForm').trigger("reset");
         },
         error: function() {
          // Fail message
           $('#success').html("<div class='alert alert-danger'>");
           $('#success > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
             .append("</button>");
           $('#success > .alert-danger').append("<strong>Sorry " + firstName + ", it seems that my mail server is not responding. Please try again later!");
        $('#success > .alert-danger').append('</div>');
         //clear all fields
         $('#contactForm').trigger("reset");
      },
    })
 },

这是contact_me.php:

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

$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];

// Create the email and send the message
$to = 'rflores@RegalMed.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:  $name";
$email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email_address\n\nMessage:\n$message";
$headers = "From: noreply@yourdomain.com\n"; // 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;            
?>

用户提交表单后,我应该能够收到包含用户名,电子邮件和评论的电子邮件。不知道我是否想念一些东西。我是否在“数据:{”部分中错误地传递了值?

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

这不是Ajax问题,因为您收到您的消息已发送。消息,位于Ajax成功回调中。

所以...您必须测试contact_me.php文件并找出问题所在。

直接从地址栏:http(s)://your-domain/mail/contact_me.php中打开以下内容:

<?php
// PHP error reporting
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

// Check for empty fields ---- Comment that out temporarily...
/*
if(empty($_POST['name'])        ||
   empty($_POST['email'])       ||
   empty($_POST['message'])     ||
   !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL)){
     echo "No arguments Provided!";
}

// Post variables
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
*/

// Temporary for direct URL tests... Remove after!
$name = "John Doh";
$email_address = "jdoh@hotmail.com";
$message = "You fond John Doh!";

// Create the email and send the message
$to = 'rflores@RegalMed.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:  $name";
$email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email_address\n\nMessage:\n$message";
$headers = "From: noreply@yourdomain.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
$headers .= "Reply-To: $email_address";

// Check if the mail function is enabled
if ( function_exists( 'mail' ) ){
  echo "mail() is available.";
}else{
  echo "mail() has been disabled";
  die();
}

// Try to sent the mail...
$mail_sent = mail($to,$email_subject,$email_body,$headers);

// Check if mail worked.
if(!mail_sent){
  echo "There was an error with the mail function<br>";
}else{
  echo "Mail() is working!";
}
?>