Hi, very new to PHP and I may be on the right track with this or totally lost.
[LIST][*]
From my index.php form, a user will enter their name, and a number associated to them, both in input boxes.
They will then select from a drop down list a course they want to take.
Once they hit the submit button I need to take their name to see if they are already enrolled and if the class is at it’s max capacity.
The student info is in student.txt ie Mike Wazowski?X-68-524 (student name and number).
The course info is in course.txt ie Animation Film Design:AFD-250:6 (course name and number and max capacity).
The search will be in enrollment.txt ie Buzz Lightyear?X-34-121:Visual Effects:VE-298.
The code I have is towards the bottom of the PHP and before the HTML starting with $datafile = ‘student.txt’;
Thank you.
[code=php]<?php
ini_set(‘display_errors’, 1);
error_reporting(E_ALL);
// Define and set variables
$msg = ”;
$submit = isset($_POST[‘submit’]) ? $_POST[‘submit’] : NULL;
$student = ”;
$studentname = isset($_POST[‘studentname’]) ? $_POST[‘studentname’] : NULL;
$studentnumber = isset($_POST[‘studentnumber’]) ? $_POST[‘studentnumber’] : NULL;
$coursefile = ‘course.txt’;
$enrollfile = ‘enrollment.txt’;
$in = fopen ($coursefile, ‘r’) or die (“course.txt cannot be opened for reading.”);
// Sanitization and Validation coding
if($submit)
{
$student = ”;
$studentfile = ‘student.txt’;
$course = isset($_POST[‘course’]) ? $_POST[‘course’] : NULL;
$coursename = ”;
$coursenumber = ”;
$coursemax = 0;
if ($submit)
{
$studentname = $_POST[‘studentname’];
$studentnumber = $_POST[‘studentnumber’];
}
if ($studentname)
{
$studentname = strip_tags ($_POST[‘studentname’]);
$studentname = htmlentities ($_POST[‘studentname’]);
$studentname = trim($_POST[‘studentname’]);
}
if ($studentnumber)
{
$studentnumber = strip_tags ($_POST[‘studentnumber’]);
$studentnumber = htmlentities ($_POST[‘studentnumber’]);
$studentnumber = trim($_POST[‘studentnumber’]);
}
if ($course)
{
$course = strip_tags ($_POST[‘course’]);
$course = htmlentities ($_POST[‘course’]);
}
// Validate student name/number against text file
function validateStudent($studentName, $studentNumber)
{
$found = false;
$fh = fopen(‘student.txt’, ‘r’);
while(($line = fgetcsv($fh, null, ‘:’)) != false) {
if(count($line) > 1)
{
if($line[0] == $studentName and $line[1] == $studentNumber)
{
$found = true;
break;
}
}
}
return $found;
}
// Validate course name/number against text file
function validateCourse($courseName, $courseNumber, $courseMax)
{
$found = false;
$fh = fopen(‘course.txt’, ‘r’);
while(($line = fgetcsv($fh, null, ‘:’)) != false) {
if(count($line) > 1) {
if($line[0] == $courseName and $line[1] == $courseNumber and $line[2] == $courseMax) {
$found = true;
break;
}
}
}
return $found;
}
}//endif submit
//$file_handle = fopen(‘enrollment.txt’, “r”);
//while (!feof($file_handle))
//{
//$line_of_text = fgets($file_handle);
//echo $line_of_text . “<BR>”;
//}
//fclose($file_handle);
//$file = ‘course.txt’;
//$FH = fopen ($file, ‘r’) or die (“$file cannot be opened for reading.”);
//while ($line = fgets ($FH)) {
//echo $line;
//echo “<br>n”;
//}
//fclose ($FH);
//**********THIS IS THE CODE AND PROCESS IN QUESTION*********
$datafile = ‘student.txt’;
// If selection has been made, find a match
if (isset ($_POST[‘studentname’])) {
$student = htmlentities ($_POST[‘studentname’]);
$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 = $student === $field[0];
}
fclose ($DB);
if ($found) {
echo “<p>$field[0] $field[1]</p>n”;
}
}
$DB = fopen ($enrollfile, ‘r’) or die (“$enrollfile cannot be opened for reading.”);
while ($record = fgets ($DB) ) {
$field = explode (“:”, htmlentities (trim ($record)));
echo “<option value=”$field[1]”>$field[0] $field[1] $field[2]</option>n”;
}
fclose ($DB);
//**************************************************************
// ND @ Webdeveloper.com
if( isset( $studentname, $studentnumber)) {
$valid = validateStudent($studentname, $studentnumber);
if($valid){
$msg = ‘<p style=”color: black; text-align: left; font-size: 15px; font-weight: bold;”>Thank you.</p>’;
} else {
$msg = ‘<p style=”color: red; text-align: left; font-size: 15px; font-weight: bold;”>**The information you have entered is not valid. Please enter your information again.**</p>’;
}
}
?>
<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>
<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 htmlentities($studentname) ?>’ required autofocus placeholder=”Please enter your first & last name using this format: Dash Parr” tabindex=”10″ size=”50″></dd>
<br>
<br>
<dt>Student Number:</dt>
<dd><input class=”inputbox” name=”studentnumber” type=”text” id=”studentnumber” value='<?php echo htmlentities($studentnumber) ?>’ required placeholder=”Please enter using this format: PX-03-046″ tabindex=”20″ size=”50″></dd>
<br>
<?php echo $msg; ?>
</dl>
<br>
</fieldset>
<br>
<fieldset><legend><strong>Course Selection</strong></legend>
<br>
Select a Course:<select name=”course” 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>