After looking at some examples, I’m not sure if I’m on the right path for this.
I am working on having the user input checked after Submit Form is pressed to ensure the Student Name and Student Number match an existing record in [B]student.txt[B]courses.txt
If the student name and student number match one of the records in [B]student.txt[B]enrollment.txt
In this section of coding under [I]// Check to see if input of $studentname matches a name in student.txt[I]//echo “<option value=”$field[1]”>$field[0] $field[1]</option>n”;
I have a PHP form with 2 user inputs, 1 user select, a clear button and submit button:
– Input box for Student Name
– Input box for Student Number
– Drop down list with 4 available courses with the course name and
course code
I have [B]courses.txt
[CODE]
Animation Film Design:AFD-250:6
Digital Sculpture:DS-410:4
History of Animation:HA-240:6
Visual Effects:VE-298:4
I have [B]student.txt
[CODE]
Riley Anderson:PX-06-009
Tinker Bell:DY-43-200
Carl Fredricksen:PX-45-767
Edith Gru:DW-21-492
Mad Hatter:DY-03-195
Buzz Lightyear: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
Sheriff Woodie:PX-32-597
[code=php]<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);
$filename = ‘test.txt’;
if(file_exists($filename)) {
if($handle = fopen($filename, ‘r’)) {
$name = array();
while(($file = fgets($handle)) !==FALSE) {
preg_match(‘#(.*):(.*)#’, $file, $match);
$array = explode(‘,’, $match[2]);
foreach($array as $val) {
$name[$match[1]][] = $val;
}
}
}
}
?>
<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>
[ATTACH]17047[/ATTACH]
Thank you
[canned-message]attachments-removed-during-migration