如何在php成功后重定向

时间:2016-07-27 17:56:14

标签: php jquery ajax

我的php需要一些帮助。我正在创建一个ajax php邮件程序系统,我试图搞清楚两件事,我想在电子邮件成功发送时这样做,重定向到另一个页面

而且,我还有另外一个问题,当我发送电子邮件时,'来自' part有我的网站主机用户名,我想把它改成我的电子邮件,任何人都知道如何使用php或ajax。

php文件

<?php

        // Only process POST reqeusts.
        if ($_SERVER["REQUEST_METHOD"] == "POST") {
            // Get the form fields and remove whitespace.
            $name = strip_tags(trim($_POST["name"]));
            $phone = trim($_POST["tel"]);
            $time = trim($_POST["time"]);
            $method = trim($_POST["method"]);
                    $name = str_replace(array("\r","\n"),array(" "," "),$name);
            $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
            $message = trim($_POST["message"]);

            // Check that data was sent to the mailer.
            if ( empty($name) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
                // Set a 400 (bad request) response code and exit.
                http_response_code(400);
                echo "Oops! There was a problem with your submission. Please complete the form and try again.";
                exit;
            }

            // Set the recipient email address.
            // FIXME: Update this to your desired email address.
            $recipient = "WebInquiry@ThePPCGroup.Com";

            // Set the email subject.
            $subject = "New contact from $name";
            $msg = "Thank you for contacting ThePPCGroup.\nSomeone will contact you shortly. Please let us know the best time to reach you, and if phone or email is better.\nThePPCGroup\n855-539-4742\nWebInquiry@ThePPCGroup.Com\nWWW.ThePPCGroup.Com";
            // Build the email content.
            $email_content = "Name: $name\n";
            $email_content .= "Email: $email\n\n";
            $email_content .= "Phone: $phone\n\n";
            $email_content .= "Prefered Time: $time\n\n";
            $email_content .= "Prefered Method: $method\n\n";
            $email_content .= "Message:\n$message\n";

            // Build the email headers.
            $email_headers = "From: $name <$email>";

            // Send the email.
            if (mail($recipient, $subject, $email_content, $email_headers)) {
                // Set a 200 (okay) response code.
                http_response_code(200);
                mail($email, "Thank You!", $msg, "WebInquiry@ThePPCGroup.Com")
            } else {
                // Set a 500 (internal server error) response code.
                http_response_code(500);
                echo "Oops! Something went wrong and we couldn't send your message.";
            }

        } else {
            // Not a POST request, set a 403 (forbidden) response code.
            http_response_code(403);
            echo "There was a problem with your submission, please try again.";
        }

    ?>

ajax文件

function sendEmail() {
    var form = $('#ajax-contact');
    var formMessages = $('#form-messages');
    var formData = $(form).serialize();


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

    $.ajax({
      type: 'POST',
      url: $(form).attr('action'),
      data: formData
    })

    .done(function(response) {
      $(formMessages).removeClass('error');
      $(formMessages).addClass('success');

      $(formMessages).text(response);
      $('#name').val('');
      $('#email').val('');
      $('#tel').val('');
      $('#time').val('');
      $('#method').val('');
      $('#message').val('');
      window.location = 'http://www.theppcgroup.com/thank-you.html';
    })

    .fail(function(data) {
      $(formMessages).removeClass('success');
      $(formMessages).addClass('error');

      if (data.responseText !== '') {
          $(formMessages).text(data.responseText);
      } else {
          $(formMessages).text('Oops! An error occured and your message could not be sent.');
      }
    });
  }

if ($('#submit').on('click', function() {
  sendEmail();
}));

1 个答案:

答案 0 :(得分:0)

您可以将数组返回到ajax函数,如下所示:

// Send the email.
            if (mail($recipient, $subject, $email_content, $email_headers)) {
                // Set a 200 (okay) response code.
                http_response_code(200);
                mail($email, "Thank You!", $msg, "WebInquiry@ThePPCGroup.Com");
                $response = array('status': true, 'message': "Thank You!");   
            } else {
                // Set a 500 (internal server error) response code.
                http_response_code(500);
                $response = array('status': false, 'message': "Oops! Something went wrong and we couldn't send your message.");                
            }
        } else {
            // Not a POST request, set a 403 (forbidden) response code.
            http_response_code(403);
            $response = array('status': false, 'message': "There was a problem with your submission, please try again.");            
        }
        return json_encode($response);

和ajax

.done(function(response) {
      $(formMessages).removeClass('error');
      $(formMessages).addClass('success');

      $(formMessages).text(response.message);
      $('#name').val('');
      $('#email').val('');
      $('#tel').val('');
      $('#time').val('');
      $('#method').val('');
      $('#message').val('');
      if (response.status == true){
       window.location = 'http://www.theppcgroup.com/thank-you.html';
      }
    })