重定向,php联系表单

时间:2016-02-10 10:23:32

标签: php html contact-form

嗨,大家好,所以我建立了一个联系人。目前,一旦用户提交了消息,我希望php脚本在提交后自动转移到网站主页...

<?php
if(isset($_POST['email'])) {
    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "email@here.co.uk";
    $email_subject = "New Message - Social Contact Form";

    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }

    // validation expected data exists
    if(!isset($_POST['name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['phone']) ||
        !isset($_POST['country']) ||
        !isset($_POST['message'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }

    $name = $_POST['name']; // required
    $email = $_POST['email']; // required
    $phone = $_POST['phone']; // required
    $country = $_POST['country']; // not required
    $message = $_POST['message']; // required
    $email_message = "Form details below.\n\n";

    function clean_string($string) {
        $bad = array("content-type","bcc:","to:","cc:","href");
        return str_replace($bad,"",$string);
    }

    $email_message .= "Name: ".clean_string($name)."\n";
    $email_message .= "Email: ".clean_string($email)."\n";
    $email_message .= "Phone: ".clean_string($phone)."\n";
    $email_message .= "Country: ".clean_string($country)."\n";
    $email_message .= "Message: ".clean_string($message)."\n";

// create email headers
$headers = 'From: '.$email_from."\r\n".
    'Reply-To: '.$email_from."\r\n" .
    'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers); 
header('Location: http://www.websitehere.com'); 
?>

<?php
}
?>

我需要添加/更改什么?

3 个答案:

答案 0 :(得分:0)

您已将文字输入更改为下拉列表。

但是,您无需更改其名称。

此外,保持选项文本和值相同。

这将确保您从用户那里获得正确的数据。

更改

 <select id="countries" name="countries" type="country" class="form-control">
<option value="GB">United Kingdom</option>

 <select id="countries" name="country" type="country" class="form-control">
<option value="United Kingdom">United Kingdom</option>

答案 1 :(得分:0)

发送电子邮件后,请通过将网址更改为您的主页来添加以下行。

header('Location: http://www.example.com/home');

有时候它会在重定向中返回错误,因此您可以在php中使用javascript,如下所示

echo "<script type='text/javascript'>window.location.href = 'http://www.example.com/home';</script>";

希望这会对你有所帮助。

答案 2 :(得分:0)

发送邮件后只需使用php header函数重定向..

...
@mail($email_to, $email_subject, $email_message, $headers);
header('Location: wherever.php');
...

如果您想向用户显示一些消息然后重定向....请使用以下功能....

....
header( "refresh:5;url=wherever.php" ); 
echo 'contact form has been submitted successfully. You\'ll be redirected in about 5 secs. '; 
....