2012-03-24 44 views
0

嗨我有一個「粘滯」形式,成功完成後,我想清除字段 我試着放置$ _POST = array();在IF語句的不同部分,成功完成後無法清除表格

這是整個文件,它是獲得真實發送到本身並記錄你在文件中寫的根目錄以外

感謝所有幫助

<!-- BEGIN CHANGABLE CONTENT --> 

<?php require('templates/header.html') ?> 
<div id="main" role="main"> 
<h1>Welcome to the site</h1> 
<h2>Please fill out the form</h2> 
<p>Register and become a member!<br />Members enjoy newsletters and free swag courtesy of Tony Browns Design</p> 

<form action="index.php" method='post' id='login_form'> 
    <legend><h2>Registration Form</h2></legend> 
    <fieldset> 
     <div> 
      <label for="fname">First Name: </label> 
      <input type="text" name='fname' id='fname' size='20' 
       value="<?php if (isset($_POST['fname'])){echo htmlspecialchars($_POST['fname']);} ?>" /> 
     </div> 
     <div> 
      <label for="lname">Last Name: </label> 
      <input type="text" name='lname' id='lname' size='20' 
       value="<?php if(isset($_POST['lname'])){echo htmlspecialchars($_POST['lname']);} ?>" /> 
     </div> 
     <div> 
      <label for="email">Email: </label> 
      <input type="text" name='email' id='email' size='20' 
       value="<?php if(isset($_POST['fname'])) {echo htmlspecialchars($_POST['email']);} ?>" /> 
     </div> 

     <div> 
      <label for="quotes" class='move-top'>Quote: </label> 
      <textarea name="quotes" id="quote" cols="22" rows="8">Enter your quotation here.</textarea><br /> 
     </div> 
     <div> 
      <input type="submit" value='submit' /> 
     </div> 
    </fieldset> 
</form> 



<?php 

$file = '../quotes_from_users.txt'; 

//Check if form is submitted 
if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
    if (empty($_POST['fname']) && (empty($_POST['lname']) && (empty($_POST['email'])))) { 
     echo '<p style="color: #d00; text-shadow: -1px -1px 0 #200; font-size: 1.4em;">You need to fill in the all the fields!</p>'; 
    } 

    if (!empty($_POST['quotes']) && ($_POST['quotes'] != 'Enter your quotation here.')) { 
     if (is_writable($file)) { 
      file_put_contents($file, $_POST['quotes'] . PHP_EOL, FILE_APPEND | LOCK_EX); 

      echo '<p style="color= #cf5 font-size: 1.4em;">Your quote has been stored.</p>'; 
      $_POST = array(); 

     } else { 
      echo '<p style="color: #d00; text-shadow: -1px -1px 0 #200;">Your quote could not be stored due to a systems error, sorry about that!</p>'; 
     } 
    } else { 
     echo '<p style="color: #d00; text-shadow: -1px -1px 0 #200; font-size: 1.4em;">Please enter a quote!</p>'; 
    } 



} 


?> 
     <?php require('templates/footer.html'); ?> 
    </div> 
<!-- END CHANGABLE CONTENT --> 
形式
+2

添加您可以包含表單文件? – MichaelRushton 2012-03-24 15:23:14

+0

我剛剛添加了新文件 – 2012-03-24 17:53:04

回答

0

您的邏輯似乎有缺陷,如果您只發布報價,它將會保存在此代碼中。

它應該是這樣的:

if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
    // the next line has changed 
    if (empty($_POST['fname']) || (empty($_POST['lname']) || (empty($_POST['email'])))) { 
     echo '<p style="color: #d00; text-shadow: -1px -1px 0 #200; font-size: 1.4em;">You need to fill in the all the fields!</p>'; 
    } 
    else // added 
    { 
    if (!empty($_POST['quotes']) && ($_POST['quotes'] != 'Enter your quotation here.')) { 
     if (is_writable($file)) { 
      file_put_contents($file, $_POST['quotes'] . PHP_EOL, FILE_APPEND); 

      echo '<p style="color= #cf5 font-size: 1.4em;">Your quote has been stored.</p>'; 
      // only empty $_POST on a successful submission 
      $_POST = array(); 


     } else { 
      echo '<p style="color: #d00; text-shadow: -1px -1px 0 #200;">Your quote could not be stored due to a systems error, sorry about that!</p>'; 
     } 
    } else { 
     echo '<p style="color: #d00; text-shadow: -1px -1px 0 #200; font-size: 1.4em;">Please enter a quote!</p>'; 
    } 
    } 
} 

這是假設你的形式獲取templates/footer.html

+0

,所以你正在改變的是AND到OR中,我試過你的解決方案,它不清除字段,表單工作除了字段沒有清除 – 2012-03-24 17:32:33

+0

哦文件被髮送到$文件../user_quotes.txt – 2012-03-24 17:44:56

+0

@pixel 67不,這是因爲在處理髮送表單之前,表單已經填入了*。在生成表單之前,您需要將處理部分移動到頂部。 – jeroen 2012-03-24 19:21:14