2013-05-08 126 views
0

我正在嘗試使用HTML,PHP和bootstrap twitter在網站上創建表單(http://youngliferaffle.com/)。我一直在瀏覽如何創建它的幾個教程,但我認爲我在位置方面遇到了問題 - 例如在用戶發生錯誤或提交答案之後重定位用戶。我也是PHP新手。我真的很感激幫助!謝謝!使用HTML和PHP創建表格

主要問題:

  • 「太多重定向到該網頁」 /可能是由於
  • 用戶不被重定向到正確的頁面提交或到期後餅乾
  • 由於沒有收到提交的電子郵件壞提交

這裏是我的HTML表單的一部分:

<form method="POST" action="contact-form-submission.php"> 
    <fieldset> 
    <label><strong>Sign-Up</strong></label> 

    <input type="text" class="name" name="cname" id="name" placeholder="Full Name"></input> 

    <input type="text" class="phone" name="phone" id="phone" placeholder="Phone Number"></input> 

    <input type="text" class="email" name="email" id="email" placeholder="Email Address"></input> 
    <div class="form-actions"> 
     <input type="submit" name="save" value="Send"> 

    </div> 
    </fieldset> 
</form> 

這是我的PHP形式

// check for form submission - if it doesn't exist then send back to contact form 
if (!isset($_POST['save']) || $_POST['save'] != 'contact') { 
    header('Location: contact-form-submission.php'); 
    exit; 
} 

// get the posted data 
$name = $_POST['contact_name']; 
$email_address = $_POST['contact_email']; 
$phone = $_POST['contact_phone']; 

// check that a name was entered 
if (empty($name)) 
    $error = 'You must enter your name.'; 
// check that an email address was entered 
elseif (empty($email_address)) 
    $error = 'You must enter your email address.'; 
// check for a valid email address 
elseif (!preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/', $email_address)) 
    $error = 'You must enter a valid email address.'; 
// check that a phone number was entered 
elseif (empty($phone)) 
    $error = 'You must enter a phone number.'; 

// check if an error was found - if there was, send the user back to the form 
if (isset($error)) { 
    //Am I putting the wrong location? 
    header('Location: contact-form-submission.php?e='.urlencode($error)); exit; 
} 

// write the email content 
$email_content = "Name: $name\n"; 
$email_content .= "Email Address: $email_address\n"; 
$email_content .= "Phone:\n\n$phone"; 

// send the email 
mail ("myemail.com", "New Contact Message", $email_content); 

// send the user back to the form 
//And This is where I'm having trouble with! the Location part 
header('Location: contact-form-submission.phps='.urlencode('Thank you for your message.')); 
exit; 
+0

那麼你有什麼具體問題? – 2013-05-08 19:38:49

+0

該腳本何時執行?是否在GET contact-form-submission.php,POST contact-form-submission.php或其他一些URL之後? – Mateusz 2013-05-08 19:39:30

+0

如果您嘗試提交表單,即使沒有正確輸入,它也會重定向您,並說有太多重定向到網頁(http://youngliferaffle.com/contact-form-submission.php)。 此外,即使我確實填寫了它,它也會表示相同,但​​我沒有收到電子郵件。 – user2363671 2013-05-08 19:40:31

回答

0

在第一行,你保持航向回到同一位置。這創建了一個循環。您需要將它們發回給表單。可能index.php?

此外,最後一行,因爲此頁面僅在發佈數據時有效,所以您需要將用戶從此頁面重定向。也許做一個新的謝謝你的頁面。

還記得你的?後.php作爲?告訴您的Web服務器不再有文件名。否則它會查找名爲thankyou.phps的文件。

// check for form submission - if it doesn't exist then send back to contact form 
if (!isset($_POST['save']) || $_POST['save'] != 'contact') { 
header('Location: index.php'); exit; 
} 

// get the posted data 
$name = $_POST['contact_name']; 
$email_address = $_POST['contact_email']; 
$phone = $_POST['contact_phone']; 

// check that a name was entered 
if (empty($name)) 
$error = 'You must enter your name.'; 
// check that an email address was entered 
elseif (empty($email_address)) 
$error = 'You must enter your email address.'; 
// check for a valid email address 
elseif (!preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/', $email_address)) 
    $error = 'You must enter a valid email address.'; 
// check that a phone number was entered 
elseif (empty($phone)) 
$error = 'You must enter a phone number.'; 

// check if an error was found - if there was, send the user back to the form 
if (isset($error)) { 
//Am I putting the wrong location? 
header('Location: contact-form-submission.php?e='.urlencode($error)); exit; 
} 

// write the email content 
$email_content = "Name: $name\n"; 
$email_content .= "Email Address: $email_address\n"; 
$email_content .= "Phone:\n\n$phone"; 

// send the email 
mail ("myemail.com", "New Contact Message", $email_content); 

// send the user back to the form 
// remember the ? after your file name! 
header('Location: thankyou.php?s='.urlencode('Thank you for your message.')); exit; 
1

最後一行

header('Location: contact-form-submission.phps='.urlencode('Thank you for your message.')); exit; 

好像你有名字吧。爲什麼重定向php文件本身。你應該使用初始表單的URL,無論名字是什麼。可能這條線會產生你得到的重定向循環錯誤。

1

那麼在PHP代碼中的第一件事是它重定向到自己。 'save'變量不會被設置,所以它無休止地重新定向。它會檢查是否設置了「保存」,但它不是第一次設置,因此它會再次重定向到同一頁面。但'save'變量不會再被設置,因爲它只是一個重定向而不是表單提交。所以它一次又一次發生,所以你得到了太多的重定向錯誤。

我通常將處理邏輯和表單保存在同一個PHP文件中。這意味着,表單的action屬性將具有與value相同的頁面URL。例如像這樣。

simple_form.php

<?php 
    //Assume the form has one field called field1 and a 'save' variable to indicate form submission 
    $field1 = ""; 

    //Declare an array to store errors 
    $errors = array(); 

    if(isset($_POST['save'])) { 
     //Form has been submitted.. do validations etc. 
     $field1 = $_POST['field1']; 
     if(someValidationCheck($field1) == false) { 
      $errors[] = "Field1 is not valid"; 
     } 

     //After all field validations.. adding errors to $errors array.. 
     if(count($errors) == 0) { 
      //No errors so write database insert statments etc. here 
      //Also put a header("Location:...") redirect here if you want to redirect to a thank you page etc. 
     } 
    } 
?> 
<html> 
<body> 
<?php 
    //If there were errors, show them here 
    if(count($errors) > 0) { 
     //loop through $errors array .. print one by one. 
    } 
?> 
<form method="post" action="simple_form.php"> 
    <input type="text" name="field1" value="<?php echo($field1); ?>" /> 
    <input type="hidden" name="save" value="save" /> 
    <input type="submit" /> 
</form> 
</body> 
</html> 

這樣,如果有錯誤,用戶將看到在同一個頁面錯誤信息,該領域也將保留其原始值。只有在有效提交時纔會重定向,而且沒有任何錯誤。否則,它將保持在同一頁面中,顯示錯誤消息。