我有一个包含2个用户输入,1个用户选择,一个清除按钮和提交按钮的PHP表单:
我有 courses.txt ,它存储了4个可用课程,其中包括课程名称,课程代码以及可以注册的最多人数:
我有 student.txt ,用于存储学生姓名的当前列表及其学号:
我正在努力在按下提交后检查用户输入以确保学生姓名和学生编号与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;