成功完成后无法清除表格

时间:2012-03-24 15:16:02

标签: php

嗨我有一个“粘性”形式,成功完成后我想清除字段 我尝试放置$ _POST = array();在IF语句的不同部分,

这是整个文件,它是一个发送给自己的表单,并记录您在webroot之外的文件中写的内容

感谢您的帮助

  <!-- 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 -->

1 个答案:

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