表单验证错误消息未显示

时间:2012-01-02 23:16:06

标签: php forms validation

希望能有一些新鲜的眼睛。我有一个页面,它将向数据库添加一个新事件。所有3个字段都是必填字段,因此如果在按下提交按钮时它们为空,则应显示错误消息。任何人都知道为什么它没有显示错误消息?提前谢谢!

以下是代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>ADMIN - Repetative Strain Injury UK</title>
    <meta http-equiv="content-type" content="application/xhtml; charset=utf-8" />
    <link href="/iis_project/view/css/mycss.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <div class="container">
        <!-- wrapper div for positioning -->
        <div class="grid10 first" id="header">
            <!-- Header Section -->
            <img src="/iis_project/view/img/banner.jpg" alt="RSI UK Banner" width="1090" height="75"/>
        </div>
        <div class="grid11 first" id="date">
            <!-- Date Section -->
            <?php
            include ("date.php");
            ?>
        </div>
        <div class="grid2 first" id="col1">
            <!-- Left column Section -->
            <h1>Navigation</h1>
            <p>
                <a href="index.php"><strong>Home</strong></a>
            </p>
            <p>
                <a href="aboutus.php"><strong>About Us</strong></a>
            </p>
            <p>
                <a href="register.php"><strong>Register</strong></a>
            </p>
            <p>
                <a href="login.php"><strong>Log In</strong></a>
            </p>
            <p>
                <a href="events.php"><strong>Events</strong></a>
            </p>
            <p>
                <a href="acknowledgements.php"><strong>Acknowledgements</strong></a>
            </p>
        </div>
        <div class="grid6" id="col2">
            <!-- Middle column Section -->
            <h1>New Event</h1>
            <p>

                As an admin you have the right to create a new event. Simply fill out the form below and hit submit.
            </p>
            <?php
// creates the new record form
// since this form is used multiple times in this file, I have made it a function that         is easily reusable
function renderForm($name, $date, $location, $error22)
{

            ?>

            <?php
            // if there are any error22s, display them
            if ($error22 != '') {
                echo '<div style="padding:4px; border:1px solid     red; color:red;">' . $error22 . '</div>';
            }
            ?>

            <form action="" method="post">
                <div>
                    Name:
                    <input type="text" name="name" value="<?php echo $name;?>" />
                    <br/>
                    Date:
                    <input type="text" name="date" value="<?php echo $date;?>" />
                    <br/>
                    Location:
                    <input type="text" name="location" value="<?php echo $location;?>" />
                    <br/>
                    <input type="submit" name="submit_event"     value="Submit">
                </div>
            </form>

<?php
}

   // connect to the database
 include ("home_connection.php");

  // check if the form has been submitted. If it has, start to process the form and    save it to the database
  if (isset($_POST['submit']))
 {
 // get form data, making sure it is valid
$name = mysql_real_escape_string(htmlspecialchars($_POST['name']));
 $date = mysql_real_escape_string(htmlspecialchars($_POST['date']));
 $location = mysql_real_escape_string(htmlspecialchars($_POST['location']));
 // check to make sure all fields are entered
 if ($name == '' || $date == '' || $location == '')
 {
 // generate error22 message
 $error22 = 'error22: Please fill in all required fields!';

 // if either field is blank, display the form again
 renderForm($name, $date, $location, $error22);
 }
 else
 {
 // save the data to the database
 mysql_query("INSERT events SET name='$name', date='$date', location='$location'")
 or die(mysql_error22());

 // once saved, redirect back to the view page
 header("Location: login_success_admin.php");
 }
 }
 else
 // if the form hasn't been submitted, display the form
 {
 renderForm('','','','');
 }
  ?>
  </div>
 <div class="grid2" id="col3">
<!-- Right column Section -->
<h1>Newsletter</h1>
<h2>Sign up to our newsletter:</h2>
<?php
include ("newsletter.php");
?>
   </div>
  <div class="grid10 first" id="footer">
<!-- Footer Section -->
<p>
    Repetative Strain Injury UK &copy; West Street, Jubille Way, Leeds, LS6  3QW. Email: <a href="info@rsiuk.org">info@rsiuk.org</a>
    Tel: 0113 658102. Reg charity no: 1032941
</p>
 </div>
 </div> <!-- end container -->
 </body>
 </html> 

2 个答案:

答案 0 :(得分:0)

尝试添加

<input type="hidden" name="submit" value="1" />

到你的表格。 $ _POST数组在您提供的代码中没有'submit'值,因此它将再次显示该表单。

答案 1 :(得分:0)

您的提交名称为“submit_event” 所以你必须使用

if (isset($_POST['submit_event'])) {
  .
  .
  .
}

希望有所帮助