New to PHP and trying to set up a course registration form using only PHP and text files, no JavaScript or MySQL. I’ve completed a few HTML contact forms with the associated PHP files but I can’t push the HTML out of my head to move forward with this.
I have index1.php set up and was hoping to paste a .jpg of it. When I call it up on the localhost it displays fine with an input box for Student Name and an input box for Student Number. At this time I have the course options in the HTML body.
[COLOR=”#FF0000″][B]My question: How can I change this to PHP to pull from a text file I’ve created with the course name and course code? Can this also be done using radio buttons?
[COLOR=”#0000FF”]index1.php code below:
[CODE]<?php
?>
<html>
<head>
<title>Registration Form</title>
<style>
body{background-color: #ffffe6; width:610px;}
h1 {color: #29a3a3;}
.inputbox {padding: 7px; border: #F0F0F0 2px solid; border-radius: 4px;}
.btn {padding: 10px;background-color: #29a3a3; border: solid thin #000000; color: #FFF; font-weight: bolder; cursor: pointer;}
</style>
</head>
<body>
<h1>Course Registration</h1>
<form name=”regfrm” method=”post” action=””>
<fieldset><legend><strong>Student Information</strong></legend>
<dl>
<dt>Student Name:</dt>
<dd><input class=”inputbox” name=”studentname” type=”text” id=”studentname” required autofocus placeholder=”Please enter your first and last name” tabindex=”10″ size=”50″></dd>
<br>
<br>
<dt for=”number”>Student Number:</dt>
<dd><input class=”inputbox” name=”studentnumber” type=”text” required id=”studentnumber” placeholder=”Please enter using the following format: PX-03-046″ tabindex=”20″ size=”50″></dd>
</dl>
<br>
</fieldset>
<br>
<fieldset><legend><strong>Available Courses</strong></legend>
<p>Please select the course you want to enrol in (select one):
<br>
<label>
<input name=”course” type=”radio” value=”afd” id=”course_0″ tabindex=”30″ >
Animation Film Design:AFD-250</label>
<br>
<label>
<input name=”course” type=”radio” value=”ds” id=”course_1″>
Digital Sculpture:DS-410</label>
<br>
<label>
<input name=”course” type=”radio” value=”ha” id=”course_2″>
History of Animation:HA-240</label>
<br>
<label>
<input name=”course” type=”radio” value=”ve” id=”course_3″>
Visual Effects:VE-298</label>
<br>
</p>
</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>
Thank you!
[code=php]
$data = json_decode(file_get_contents('name_of_file.json'), true);
[/code]
$found = FALSE;
while ($record = fgets ($DB) and ! $found) {
$field = explode (",", htmlentities (trim ($record)));
$found = $course ==+ $field[0];
}
fclose ($DB);
if ($found) {
echo "<p>You have selected: $field[0] $field[1]</p>n";
}
<fieldset><legend><strong>Student Information</strong></legend>
<dl>
<dt>Student Name:</dt>
<dd><input class="inputbox" name="studentname" type="text" id="studentname" required autofocus placeholder="Please enter your first and last name" tabindex="10" size="50"></dd>
<br>
<br>
<dt for="number">Student Number:</dt>
<dd><input class="inputbox" name="studentnumber" type="text" required id="studentnumber" placeholder="Please enter using the following format: PX-03-046" tabindex="20" size="50"></dd>
</dl>
<br>
</fieldset>
<br>
<fieldset><legend><strong>Available Courses</strong></legend>
<br>
$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[0]">$field[1]</option>n";
echo " </select>n";
<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>
[code=php]<?php
// Generate the form
echo " Select a Course: <select name="course">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[0]">$field[1]</option>n";
}
fclose ($DB);
echo " </select>n";
?> [/code]
[code=php]Select a Course: <select name="course">
<option value="-1" selected>Please Select...</option>
<?php
// Get the data
$DB = fopen ($datafile, 'r') or die ("$datafile cannot be opened for reading.");
// I will assume this bit works after the change...
while ( !feof($DB) ) {
// use a CSV function to pull the information rather than explode.
$field = fgetcsv($DB);
// use sprintf formatting to make complex output simpler, use PHP_EOL for end of line
echo sprintf(" <option value='%s'>%s</option>%s", $field[0], $field[1], PHP_EOL );
}
fclose ($DB);
?>
</select>
[/code]
[code=php]echo sprintf(" <option value='%s'>%s</option>%s", $field[0], $field[1], PHP_EOL );[/code]
[code=php]<?php
// Convert courses.txt file to comma delimited file coursesfinal.txt
$in = "courses.txt";
$out = "coursesfinal.txt";
$IN = fopen ($in, 'r') or die ("$in cannot be opened for reading.");
$OUT = fopen ($out, 'w') or die ("$out cannot be opened for writing.");
if (flock($OUT, LOCK_EX)) {
while ($inline = fgets ($IN) ) {
$splitarray = explode (":", $inline);
$outline = implode(",", $splitarray);
fputs ($OUT, $outline);
}
flock($OUT, LOCK_UN);
}
fclose ($IN);
fclose ($OUT);
// Search coursesfinal.txt file for course to match user input
$datafile = "coursesfinal.txt";
// If selection has been made, find a match
if (isset ($_POST['course'])) {
$course = htmlentities ($_POST['']);
$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 = $course === $field[0];
}
fclose ($DB);
if ($found) {
echo "<p>You have selected: $field[0] $field[1]</p>n";
}
}
?>
<html>
<head>
<title>Registration Form</title>
<style>
body{background-color: #ffffe6; width:610px;}
h1 {color: #29a3a3;}
.inputbox {padding: 7px; border: #F0F0F0 2px solid; border-radius: 4px;}
.btn {padding: 10px;background-color: #29a3a3; border: solid thin #000000; color: #FFF; font-weight: bolder; cursor: pointer;}
</style>
</head>
<body>
<h1>Course Registration</h1>
<form name="regfrm" method="post" action="">
<fieldset><legend><strong>Student Information</strong></legend>
<dl>
<dt>Student Name:</dt>
<dd><input class="inputbox" name="studentname" type="text" id="studentname" required autofocus placeholder="Please enter your first and last name" tabindex="10" size="50"></dd>
<br>
<br>
<dt for="number">Student Number:</dt>
<dd><input class="inputbox" name="studentnumber" type="text" required id="studentnumber" placeholder="Please enter using the following format: PX-03-046" tabindex="20" size="50"></dd>
</dl>
<br>
</fieldset>
<br>
<fieldset><legend><strong>Available Courses</strong></legend>
<br>
Select a Course: <select name="course">
<option value="-1" selected>Select From...</option>
<?php
// Get the data
$DB = fopen ($datafile, 'r') or die ("$datafile cannot be opened for reading.");
// I will assume this bit works after the change...
while ( !feof($DB) ) {
// use a CSV function to pull the information rather than explode.
$field = fgetcsv($DB);
// use sprintf formatting to make complex output simpler, use PHP_EOL for end of line
echo sprintf("<option value='%s'>%s</option>%s", $field[0], $field[1], PHP_EOL );
}
fclose ($DB);
?>
</select>
<br>
<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>[/code]
as for the form... you need to set the enctype for the form if you are POSTing your form.
This bit I would do a bit differently
[code=php]<?php
// Generate the form
echo " Select a Course: <select name="course">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[0]">$field[1]</option>n";
}
fclose ($DB);
echo " </select>n";
?> [/code]
I would do it more like.
[code=php]Select a Course: <select name="course">
<option value="-1" selected>Please Select...</option>
<?php
// Get the data
$DB = fopen ($datafile, 'r') or die ("$datafile cannot be opened for reading.");
// I will assume this bit works after the change...
while ( !feof($DB) ) {
// use a CSV function to pull the information rather than explode.
$field = fgetcsv($DB);
// use sprintf formatting to make complex output simpler, use PHP_EOL for end of line
echo sprintf(" <option value='%s'>%s</option>%s", $field[0], $field[1], PHP_EOL );
}
fclose ($DB);
?>
</select>
[/code]
It would be much better if you use a database like MySQL if you can't use MySQL for whatever reason, porting your data file in to an SQLite3 database, means you can simply get the advantages of a database within your script and use SQL type queries, easily update, etc.[/QUOTE]
flat file.
[code=php]
echo sprintf("stuff goes here");
printf("stuff goes here");
[/code]
...
[/noparse] bbcode tags here) followed by a representation of how you want that data output (likely within [noparse][code=html]...[/code]
[/noparse] tags) would make life a bit simpler for those of us who'd like to help, but have had a long weekend and are inherently lazy on top of that. ?Animation Film Design,AFD-250,6
Digital Sculpture,DS-410,4
History of Animation,HA-240,6
Visual Effects,VE-298,4
[code=php]
echo "<input type='radio' name='course' value='$course_code'> $course_name $course_code<br>";
[/code]
As to how to display something this line works just as well as sprint or printf:
echo "stuff goes here";
[/QUOTE]
[code=php]
$fh = fopen('/path/to/course_file.txt', 'r');
while(($row = fgetcsv($fh)) != false) {
if(count($row == 3) {
printf(
"<input type='radio' name='course' value='%s'>%s %s<br>n",
htmlentities($row[1]),
htmlentities($row[0]),
htmlentities($row[1])
);
}
}
[/code]
[code=php]
$fh = fopen('/path/to/course_file.txt', 'r');
while(($row = fgetcsv($fh)) != false) {
if(count($row == 3) {
printf(
"<input type='radio' name='course' value='%s'>%s %s<br>n",
htmlentities($row[1]),
htmlentities($row[0]),
htmlentities($row[1])
);
}
}
[/code]
[/QUOTE]
[code=php]<?php
// Generate the form
//echo " Select a Course: <select name="course">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[0]">$field[1]</option>n";
}
fclose ($DB);
echo " </select>n";
?>[/code]
[code=php]<?php
// Generate the form
//echo " Select a Course: <select name="course">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[0]">$field[1]</option>n";
}
fclose ($DB);
echo " </select>n";
?>[/code]
[code=php]Select a Course: <select name="course">
<option value="-1" selected>Select From...</option>
<?php
// Generate the form
$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);
echo " </select>n";
?>[/code]
Would this code replace...
[code=php]<?php
[/QUOTE]
// Generate the form
//echo " Select a Course: <select name="course">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[0]">$field[1]</option>n";
}
fclose ($DB);
echo " </select>n";
?>[/code]
One thing in your explode line: The trim function will only trim the spaces from the start and the end of the entire $record string and NOT each quasi-field in that string. If you need to trim each resulting array element you should do that on the array instead of the just-read string.
For Future Reference:
One should really practice separating the presentation code (html) from the logic code (the PHP). Look at your current scriptlet. You start right out by sending out some html. Then you start doing the data retrieval and generation of the output from that data. But wait - what if your file open fails to happen and your code then fails to do anything? You have a hanging dropdown on your client screen. How do you handle that?
Do your php code first so that you know for sure what you will be able to output. In your case open the file and build a php var to contain the output you wish to generate from that process. Continue with your other dropdowns generating new php vars. When you have finishing 'building' your page, THEN start doing your output of the html with these php vars inserted where they belong.
Pseudo code:
(open the first data file and create a var $dropdown1 that will contain the html from <select> to </select>)
(repeat for other dropdowns and create $dropdown1, $dropdown3, etc)
(Output your entire html code with the above $dropdownx vars stuck in the right place to create the output image that you want to see.)[/QUOTE]
[code=php]<?php
function trim_value(&$value)
{
$value = trim($value);
}
$fruit = array('apple','banana ', ' cranberry ');
var_dump($fruit);
array_walk($fruit, 'trim_value');
var_dump($fruit);
?>[/code]
[code=php]
// this should be defined outside of any loops or ifs:
function clean($text, $charset = 'UTF-8')
{
return htmlentities(trim($text), ENT_QUOTES | HTML401, $charset);
}
// usage:
echo ' <option value="'.clean($field[1]).'">'.clean($field[0]).' '.clean($field[1])."</option>n";
// or (in this case easier to read, I think):
printf(
" <option value="%s">%s %s</option>n",
clean($field[1]),
clean($field[0]),
clean($field[1])
);
[/code]
[code=php]<html>
<head>
<title>Registration Form</title>
<style>
body{background-color: #ffffe6; width:610px;}
h1 {color: #29a3a3;}
.inputbox {padding: 7px; border: #F0F0F0 2px solid; border-radius: 4px;}
.btn {padding: 10px;background-color: #29a3a3; border: solid thin #000000; color: #FFF; font-weight: bolder; cursor: pointer;}
</style>
<?php
// Sanitization and Validation coding will go here
// Initialize and set variables
$studentname = " ";
$studentnumber = " ";
$courses = " ";
$datafile = "coursesfinal.txt";
// Convert courses.txt file to comma delimited file coursesfinal.txt
$in = "courses.txt";
$out = "coursesfinal.txt";
$IN = fopen ($in, 'r') or die ("$in cannot be opened for reading.");
$OUT = fopen ($out, 'w') or die ("$out cannot be opened for writing.");
if (flock($OUT, LOCK_EX)) {
while ($inline = fgets ($IN) ) {
$splitarray = explode (":", $inline);
$outline = implode(",", $splitarray);
fputs ($OUT, $outline);
}
flock($OUT, LOCK_UN);
}
fclose ($IN);
fclose ($OUT);
// Generate the form
$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"; //Moved to the Select a Course fieldset below
}
fclose ($DB);
?>
</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 $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 echo " <option value="$field[1]">$field[0] $field[1]</option>n"; ?>
</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>[/code]
Pardon me if I missed this earlier, but why to you read from the one file and immediately write to another file? Unless I'm missing something (which I may have), you should only need to read the data in from the "in" file. Then the option is to either write the fields into an array (probably an array of arrays in this case), or somewhat simpler (at least in terms of amount of code) is to just read/parse each line as you're ready to use it in the form output.
To that end, you'll need [i]some[/i] kind of loop to set each option in the select element, either a foreach loop on the aforementioned array, or a while loop on the reading of the text file line by line.[/QUOTE]
[code=php]
<html>
<head>
<title>Registration Form</title>
<style>
body{background-color: #ffffe6; width:610px;}
h1 {color: #29a3a3;}
.inputbox {padding: 7px; border: #F0F0F0 2px solid; border-radius: 4px;}
.btn {padding: 10px;background-color: #29a3a3; border: solid thin #000000; color: #FFF; font-weight: bolder; cursor: pointer;}
</style>
</head>
<body>
<?php
// Sanitization and Validation coding will go here
// Initialize and set variables
$studentname = " ";
$studentnumber = " ";
$courses = " ";
$datafile = "coursesfinal.txt";
$in = fopen ('course.txt', 'r') or die ("courses.txt cannot be opened for reading.");
?>
<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(($field = fgetcsv($in, null, ':')) != false) {
if (count($row) > 1) {
echo "
<option value="$field[1]">$field[0] $field[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>
[/code]
Nah, I'd just read it in and use what you get within the script: processing data in RAM is much more efficient than disk I/O.
No guarantees this works, but it's just things rearranged a bit and tightened up to remove superfluous code -- and maybe even work:
[code=php]
[/QUOTE]
<html>
<head>
<title>Registration Form</title>
<style>
body{background-color: #ffffe6; width:610px;}
h1 {color: #29a3a3;}
.inputbox {padding: 7px; border: #F0F0F0 2px solid; border-radius: 4px;}
.btn {padding: 10px;background-color: #29a3a3; border: solid thin #000000; color: #FFF; font-weight: bolder; cursor: pointer;}
</style>
</head>
<body>
<?php
// Sanitization and Validation coding will go here
// Initialize and set variables
$studentname = " ";
$studentnumber = " ";
$courses = " ";
$datafile = "coursesfinal.txt";
$in = fopen ('course.txt', 'r') or die ("courses.txt cannot be opened for reading.");
?>
<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(($field = fgetcsv($in, null, ':')) != false) {
if (count($row) > 1) {
echo "
<option value="$field[1]">$field[0] $field[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>
[/code]
<i>
</i>Animation Film Design:AFD-250:6
Digital Sculpture:DS-410:4
History of Animation:HA-240:6
Visual Effects:VE-298:4
[code=php]
<html>
<head>
<title>Registration Form</title>
<style>
body{background-color: #ffffe6; width:610px;}
h1 {color: #29a3a3;}
.inputbox {padding: 7px; border: #F0F0F0 2px solid; border-radius: 4px;}
.btn {padding: 10px;background-color: #29a3a3; border: solid thin #000000; color: #FFF; 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
// Initialize and set variables
$studentname = " ";
$studentnumber = " ";
$courses = " ";
$in = fopen ('courses.txt', 'r') or die ("courses.txt cannot be opened for reading.");
?>
<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>
[/code]
0.1.9 — BETA 5.18