-1

我需要使用数据库和空字段验证表单。我制作了一个带有空字段过滤器的表单,因此如果用户跳过填写字段,它将被重定向回来,用户只需在之前输入一个空字段。我需要检查数据库是否已经存在,如果不存在,用户必须填写所有字段。

这是代码

<?php
session_start();
if(isset($_SESSION['error']))
{
    $error = $_SESSION['error'];
    $_POST = $_SESSION['post'];
    unset($_SESSION['error']);
    unset($_SESSION['post']);
}
?>
<head>
<script type="text/javascript" src="jquery-1.5.1.min.js"></script>
<script type="text/javascript" src="jquery-ui-1.8.11.custom.min.js"></script>
<link rel="stylesheet" type="text/css" href="jquery-ui-1.8.11.custom.css">
<script type="text/javascript">
    $(document).ready(function()
    {
        $("#date").datepicker(
        {
             dateFormat: "dd-mm-yy",
             changeMonth: true,
             changeYear: true,
             yearRange: "-0:+100"
        });
     });
</script>
</head>
<form name="form" method="post" action="process.php">
<table>
    <tr>
        <td>Name:</td>
        <td>
             <input name="name" id="name" type="text" value="<?php echo isset($_POST['name']) ? $_POST['name'] : '';?>"/>
             <div style="color:red"><?php echo isset($error['name']) ? $error['name'] : '';?></div>
        </td>
    </tr>
    <tr>
         <td>Phone:</td>
         <td>
              <input name="phone" id="phone" type="text" value="<?php echo isset($_POST['phone']) ? $_POST['phone'] : '';?>"/>
              <div style="color:red"><?php echo isset($error['phone']) ? $error['phone'] : '';?></div>
         </td>
    </tr>
    <tr>
         <td>Date:</td>
         <td>
              <input name="date" id="date" type="text" value="<?php echo isset($_POST['date']) ? $_POST['date'] : '';?>"/>
              <div style="color:red"><?php echo isset($error['date']) ? $error['date'] : '';?></div> 
         </td>
    </tr>
    <tr>
         <td>Time:</td>
         <td>
              <input name="time" id="time" type="text" value="<?php echo isset($_POST['time']) ? $_POST['time'] : '';?>"/>
              <div style="color:red"><?php echo isset($error['time']) ? $error['time'] : '';?></div>
         </td>
    </tr>
    <tr>
         <td>
              <input name="submit" id="submit" type="Submit" value="Submit" />
         </td>
    </tr>
</table>
</form>

这是php进程文件

<?php
session_start();
if($_POST)
{
   $name = $_POST['name'];
   $phone = $_POST['phone'];
   $date = $_POST['date'];
   $time = $_POST['time'];
   $error = array();
   if(empty($name)){
      $error['name'] = 'Name must be filled';
   }
   if(empty($phone)){
      $error['phone'] = 'Phone Number must be filled'; 
   }
   if(empty($date)){
      $error['date'] = 'Date must be filled';
   }
   if(empty($time)){
      $error['time'] = 'Time must be filled';
   }
   if(empty($error))
   {
      $check = mysql_query("SELECT * FROM event WHERE event='$time' AND date='$date'");
      $in = mysql_num_rows($check);
      if($in > 0)
      {
          $error['date'] = 'Select other date';
          echo '<script type="text/javascript">';
          echo 'alert("Sorry, it is already filled\n Please select the other date");';
          echo 'window.location.href="cal.php";';
          echo '</script>';
      }
      else
      {
           //insert into database
      }
   }else
   {
      $_SESSION['error'] = $error;
      $_SESSION['post'] = $_POST;
      header("location: cal.php");
   }
}
4

1 回答 1

2

我希望以下更改可以解决您的问题。

请添加 $_SESSION['post'] = $_POST; process.php 中 Javascript 声明之前的语句。

这段代码

 $error['date'] = 'Select other date';
 echo '<script type="text/javascript">';
 echo 'alert("Sorry, it is already filled\n Please select the other date");';
 echo 'window.location.href="cal.php";';
 echo '</script>';

更改为

$error['date'] = 'Select other date';
$_SESSION['post'] = $_POST;    // extra line added
echo '<script type="text/javascript">';
echo 'alert("Sorry, it is already filled\n Please select the other date");';
echo 'window.location.href="cal.php";';
echo '</script>';
于 2013-06-24T05:36:35.450 回答