1

我在联系表格方面遇到了各种各样的问题。当我在我的家庭服务器上进行测试时,一切运行顺利。一旦我上传到网上,它就不起作用了。首先有标题问题,现在显然“这个网页有一个重定向循环”。

这是我的代码。请告诉我该怎么做。谢谢。

<?php
// Title: Contact Form - Dolce Forno GB
// Updated: 5/9/2012

//Validation code
if (!empty($_POST)) {
$errors = array();

//variables
$name      = $_POST['name'];
$email     = $_POST['email'];
$phone     = $_POST['phone'];
$subject   = $_POST['subject'];
$message   = $_POST['message'];

//All field are required
if (empty($name) === true || empty($email) === true || empty($phone) === true ||   empty($subject) === true || empty($message) === true ){
    $errors[] = 'Please fill in all the fields.';
}
    else {
        //This regex allows only: a-z,A-Z, space, comma, full stop, apostrophe, dash
        if (!preg_match("/^[a-zA-Z\s,.'-]+$/", $name)) { 
            $errors[] = 'Invalid name.'; 
            /*die ("Invalid name."); */
        }
        //var_filter php function
        if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
            $errors[] = 'Invalid email address.'; 
        }
        //This regex allows only: 0-9, space, dash, brackets, min-max length: 10-15
        if (!preg_match("/^[0-9\s]{10,15}$/", $phone)){
            $errors[] = 'Invalid phone number.';
        }
    }
}
if (empty($errors)) {
    //send email
    mail('info@dolcefornogb.com', 'Contact Form', $subject, 'Message:' . $message,'From: ' . $name . $email . $phone);
    header('Location:mail.php?sent');
    exit ();
}
print_r($errors);

?>

<DOCTYPE html>
<html>
<head>
</head>
<body>
<?php
if (isset($_GET['sent']) === true) {
    echo '<p>Thanks for contacting us!</p>';
}
else {
    if (!empty($errors)){
        echo    '<ul>';

        foreach ($errors as $error){
            echo '<li>', $error,'</li>';

        echo    '</ul>';
        }
    }
?>
        <form action="" method="post">

        <p> <label for="name">Name 
        <span class="small">Add your name </span></label> 
        <input type="text" name="name" id="name" 
        <?php 
        if(isset($_POST['name']) === true){
            echo 'value="', strip_tags($_POST['name']),'"';
        }
        ?>
        >
        </p>
        <p> <label for="email">E-mail address
        <span class="small"> Add your e-mail</span></label> 
        <input type="text" name="email" id="email"
        <?php 
        if(isset($_POST['email']) === true){
            echo 'value="', strip_tags($_POST['email']),'"';
        }
        ?>          
        >
        </p>
        <p><label for="phone">Phone<span class="small"> Add your phone number</span></label> 
        <input type="text" name="phone" id="phone"
        <?php 
        if(isset($_POST['phone']) === true){
            echo 'value="', ($_POST['phone']),'"';
        }
        ?>
        >
        </p>
        <p><label for="suject">Subject </label> 
        <input type="text" name="subject" id="subject"
        <?php 
        if(isset($_POST['subject']) === true){
            echo 'value="', strip_tags($_POST['subject']),'"';
        }
        ?>
        >
        </p>
        <p><label for="message">Message:</label>
        <textarea name="message" id="messgae" rows="10" cols="50"> 
        <?php 
        if(isset($_POST['message']) === true){
            echo strip_tags($_POST['message']);
        }
        ?></textarea> 
        </p>
        <p><label for="call">Request Phone Call</label>
        Yes:<input type="radio" value="Yes" name="call">
        No:<input type="radio" value="No" name="call">
        </p>
        <p class="buttons">
        <input type="submit" value="Send"> <input type="reset" value="Clear">
        </p>
        </form>
<?php       
}

?>

4

1 回答 1

0

尝试重定向到仅包含成功消息的其他页面。

即替换

header('Location:mail.php?sent');

header('Location:mail-success.php?sent');

然后摆脱(移动到新页面)

if (isset($_GET['sent']) === true) {
    echo '<p>Thanks for contacting us!</p>';
}

还尝试将 303 状态添加到标头调用

http://www.electrictoolbox.com/php-303-redirect/

于 2012-10-05T09:26:20.580 回答