联系表格问题

时间:2012-10-05 09:16:10

标签: php forms contact

我在联系表格中遇到各种各样的问题。当我在家用服务器上测试时,一切运行顺利。一旦我在线上传它就无法运行。首先是标题出现问题,现在显然是“此网页有重定向循环”。

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

<?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       
}

?>

1 个答案:

答案 0 :(得分: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/