2012-01-02 243 views
0

希望对此有一些新鲜的眼光。我有一个页面将添加一个新的事件到数据库。所有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="[email protected]">[email protected]</a> 
    Tel: 0113 658102. Reg charity no: 1032941 
</p> 
</div> 
</div> <!-- end container --> 
</body> 
</html> 
+0

我建议PHP从HTML代码中分离,以便更好地理解。 – 2012-01-02 23:21:05

+0

尝试在表单提交后回显变量,看看它们是否确实有任何内容。你总是可以用if(strlen($ name)<1 ||'等)替换if($ name ==''||'etc)来查看是否有效。 – James 2012-01-02 23:24:29

回答

0

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

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

希望它可以帮助

0

尝试增加

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

到表单中。 $ _POST数组在您提供的代码中没有“提交”值,所以它只是再次显示表单。