2012-10-05 27 views
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('[email protected]', '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  
} 

?> 

+0

您不必退出後重定向用戶。 –

+0

使用Firebug或Chrome開發工具,當你提交表單,看看它是否是一個循環重定向觀看請求。 @CanVural exit()並沒有錯,它在重定向後出現。 – MrCode

回答

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/

+0

我迷路了。你知道任何好的指導如何創建一個PHP聯繫表單嗎? – Veronika

+0

http://www.html-form-guide.com/contact-form/php-contact-form-tutorial.html包括示例代碼,看起來OK – chim

+0

我試過,但有是已被棄用像eregi和拆分功能在代碼中。還有一個問題:不能修改標題信息 - 已經發送的標題。 – Veronika