0

首先,我应该说我对 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
});

});

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

4

2 回答 2

1

假设 PHP 代码是使用 AJAX 调用的,您可以将此行添加到 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";
于 2013-08-04T23:00:22.107 回答
0

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

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

这样这个字符串就会在表单成功提交到 ajax 函数时返回,并且在执行时用户将被重定向到下一个位置。

于 2013-08-04T23:01:19.173 回答