2012-03-24 122 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