php redirect谢谢你页面

时间:2013-08-04 22:49:27

标签: php html phpmailer

首先我应该说我对php一无所知,而且我正试图自己修改我网站上的内容。我希望用户在提交联系表单后被重定向到感谢页面。我一直在通过所有其他答案阅读有关此主题的内容,但仍然没有得到。

我的HTML代码是:

<div id="form_container">

<div id="main">
    <p><small>Name</small> <input type="text" name="name" id="name" /></p>
    <p><small>Email</small> <input type="text" name="email" id="email" /></p>
    <p><small>Phone</small> <input type="text" name="phone" id="phone" /></p>
    <p><small>Message</small> <textarea name="message" id="message" rows="12"></textarea></p>   
    <p><input type="submit" name="submit" id="submit" value="Email Us!" /></p>
    <ul id="response" />
</div><!--end main -->

我的PHP代码是:

<?php

    $name = trim($_POST['name']);
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $message = stripslashes($_POST['message'] . "<br>Phone Number: " . $_POST['phone']);  

    $site_owners_email = 'xxxxxxx'; // Replace this with your own email address
    $site_owners_name = 'xxxxxxxxx'; // replace with your name

    if (strlen($name) < 2) {
        $error['name'] = "Your name is.....";   
    }


    if (!preg_match('/^[a-z0-9&\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
        $error['email'] = "Invalid email address";  
    }

    if (strlen($phone) < 10) {
        $error['phone'] = "Your phone number?"; 
    }

    if (strlen($message) < 5) {
        $error['message'] = "Your message?";
    }

    if (!$error) {

        require_once('phpMailer/class.phpmailer.php');
        $mail = new PHPMailer();

        $mail->From = $email;
        $mail->FromName = $name;
        $mail->Subject = "Website Contact Form";
        $mail->AddAddress($site_owners_email, $site_owners_name);
        $mail->AddAddress('xxxxxxxxxx', 'xxxxxxxx');


        $mail->Body = $message


        $mail->Send();

        echo "<li class='success'> Congratulations, " . $name . ". We received your message</li>";

    } # end if no error
    else {

        $response = (isset($error['name'])) ? "<li>" . $error['name'] . "</li> \n" : null;
        $response .= (isset($error['email'])) ? "<li>" . $error['email'] . "</li> \n" : null;
        $response .= (isset($error['phone'])) ? "<li>" . $error['phone'] . "</li> \n" : null;
        $response .= (isset($error['message'])) ? "<li>" . $error['message'] . "</li>" : null;

        echo $response;
    } # end if there was an error sending

?>

我的js文件是

$(function() {
var paraTag = $('input#submit').parent('p');
$(paraTag).children('input').remove();
$(paraTag).append('<input type="button" name="submit" id="submit" value="Email Us Now!" />');

$('#main input#submit').click(function() {
    $('#main').append('<img src="img/ajax-loader.gif" class="loaderIcon" alt="Loading..." />');

    var name = $('input#name').val();
    var email = $('input#email').val();
    var phone = $('input#phone').val();
    var message = $('textarea#message').val();

    $.ajax({
        type: 'post',
        url: 'sendEmail.php',
        data: 'name=' + name + '&email=' + email + '&phone=' = phone + '&comments=' + comments,

        success: function(results) {
            $('#main img.loaderIcon').fadeOut(1000);
            $('#response').html(results);
        }
    }); // end ajax
});

});

我想要的只是用户在填写表单后看到一个简单的感谢页面。

2 个答案:

答案 0 :(得分:1)

假设使用AJAX调用PHP代码,您可以将此行添加到AJAX请求的成功部分。

window.location.href = '/path/to/your/thankyou/page.html';

仅当sendEmail.php页面的结果显示邮件已发送时。一种方法是仅在发送消息时使sendEmail.php回显“OK”。然后你可以做这样的事情:

success: function(results) {
    $('#main img.loaderIcon').fadeOut(1000);
    if (results == "OK") {
        window.location.href = '/path/to/your/thankyou/page.html';
    }
}

要使sendEmail.php仅在发送消息时返回“OK”,请更改此行:

echo "<li class='success'> Congratulations, " . $name . ". We received your message</li>";

用这个:

echo "OK";

答案 1 :(得分:0)

如果在php中正确验证了表单并且没有找到错误,则需要将这行代码添加到php

echo "<script>window.location="http://www.location.com/other.htm"</script>";

以便在成功将表单提交到ajax函数时返回此字符串,并在执行时将用户重定向到下一个位置。