PHP表单:检查用户输入是否与.txt文件中的值匹配

时间:2016-03-11 00:33:25

标签: php forms text user-input

我有一个包含2个用户输入,1个用户选择,一个清除按钮和提交按钮的PHP表单:

  • 学生姓名的输入框
  • 学号的输入框
  • 下拉列表包含4个课程名称和课程 课程代码

我有 courses.txt ,它存储了4个可用课程,其中包括课程名称,课程代码以及可以注册的最多人数:

  • 动画电影设计:AFD-250:6
  • 数字雕塑:DS-410:4
  • 动画史:HA-240:6
  • 视觉效果:VE-298:4

我有 student.txt ,用于存储学生姓名的当前列表及其学号:

  • Riley Anderson:PX-06-009
  • 小叮当:DY-43-200
  • Carl Fredricksen:PX-45-767
  • Edith Gru:DW-21-492
  • Mad Hatter:DY-03-195
  • 巴斯光年:PX-34-121
  • Stuart Little:CP-17-199
  • Bob Minion:DW-34-628
  • Thomas O' Malley:DY-21-987
  • Kitty Softpaws:DW-07-201
  • James Sullivan:PX-01-111
  • Mike Wazowski:PX-68-524
  • 警长伍迪:PX-32-597

我正在努力在按下提交后检查用户输入以确保学生姓名和学生编号与student.txt中的现有记录匹配,并且所选课程是courses.txt中的现有课程,并且最大注册编号具有没有达到。

如果学生姓名或学号与现有记录不符,则不得告知学生哪部分学生信息不匹配。

如果学生姓名和学生编号与student.txt中的某个记录匹配,并且所选课程未达到最大注册,并且如果学生尚未注册该课程,则该学生可以注册该课程。



<html>
<head>
    <title>Registration Form</title>
    <style>
        body{background-color: #ffffe6; width:610px;}
        h1 {color: #29a3a3;}
        .inputbox {padding: 7px; border: #FF9966 1px solid; border-radius: 4px;}
        .btn {padding: 10px; background-color: #29a3a3; border: solid 1px #FF9966; border-radius: 4px; color: #FFFFFF; font-weight: bolder; cursor: pointer;}
    </style>
</head>
<body>
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
// Sanitization and Validation coding will go here


// Define and set variables
$studentname = "";
$studentnumber = "";
$courses = " ";
$datafile = "student.txt";
$in = fopen ('courses.txt', 'r') or die ("courses.txt cannot be opened for reading.");


// Check to see if input of $studentname matches a name in student.txt
if (isset ($_POST['student.txt'])) {
    $studentname = htmlentities ($_POST['student.txt']);
    $DB = fopen ($datafile, 'r') or die ("$datafile cannot be opened for reading.");

    $found = FALSE;
    while ($record = fgets ($DB) and ! $found) {
       $field = explode (":", htmlentities (trim ($record)));
       $found = $studentname === $field[0];
    }

    fclose ($DB);

    if ($found) {
       echo "<p>$field[0] $field[1].</p>\n";
    }
 }

$DB = fopen ($datafile, 'r') or die ("$datafile cannot be opened for reading.");
 while ($record = fgets ($DB) ) {
    $field = explode (":", htmlentities (trim ($record)));
    //echo "   <option value=\"$field[1]\">$field[0] $field[1]</option>\n";
 }
 fclose ($DB);
 

 


?>

<h1>Course Registration</h1>
<form method="post" action="index.php">
    <fieldset><legend><strong>Student Information</strong></legend>
        <dl>
            <dt>Student Name:</dt>
            <dd><input class="inputbox" name="studentname" type="text" id="studentname" value="<?php echo $studentname;?>" required autofocus placeholder="Please enter your first and last name" tabindex="10" size="50"></dd>
            <br>
            <br>
            <dt>Student Number:</dt>
            <dd><input class="inputbox" name="studentnumber" type="text" id="studentnumber" value="<?php echo $studentnumber;?>" required placeholder="Please enter using the following format: PX-03-046" tabindex="20" size="50"></dd>
        </dl>
        <br>
    </fieldset>
    <br>
    <fieldset><legend><strong>Course Selection</strong></legend>
        <br>
        Select a Course:<select name="courses" tabindex="30">\n";
            <option value="-1" >Available Courses...</option>
                <?php
                while(($fields = fgetcsv($in, null, ':')) != false) {
                    if (count($fields) > 1) {
                        echo "
                            <option value=\"$fields[1]\">$fields[0] $fields[1]</option>";
                    }
                }
                ?>
        </select>
        <br>
        <br>
        <br>
        <br>
        <br>
        <br>
    </fieldset>
    <div>
        <p>
            <input name="reset" type="reset" tabindex="40" value="Clear Form" class="btn">
            <input name="submit" type="submit" tabindex="50" value="Submit Form" class="btn">
        </p>
    </div>
</form>
</body>
</html>
&#13;
&#13;
&#13;

0 个答案:

没有答案