简体   繁体   中英

On PHP form, drop down list displays wrong data from .txt file

I looked at related posts on this and saw one that now has me thinking I've done this incorrectly. I'm very, very new to PHP but willing to learn.

I need to have an index.php form with an input box for Student Name, and input box for Student Number and a drop down list of courses.

My PHP scripts are not allowed to contain data - the data must be retrieved from .txt files which are not optional.

I have courses.txt that has four course names, each with thier own unique course code and the max number of people that can enrol. Contents of courses.txt: Animation Film Design:AFD-250:6 Digital Sculpture:DS-410:4 History of Animation:HA-240:6 Visual Effects:VE-298:4

I have coursesfinal.txt which has been converted to a comma delimited file. Contents of coursesfinal.txt: Animation Film Design,AFD-250,6 Digital Sculpture,DS-410,4 History of Animation,HA-240,6 Visual Effects,VE-298,4

Currently the two input boxes work fine. My problem is with the drop down list. I would like it to display the course name then a space and then the course code. At this time it displays the course code only. I also don't understand why it is displaying the second field of data.

Thank you.

index.php code...

 <?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['courses'])) { $courses = strip_tags ($_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 = $courses === $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 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" 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 // 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[0]\\">$field[1]</option>\\n"; } fclose ($DB); echo " </select>\\n"; ?> <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> 

See your following code:

while ($record = fgets ($DB) ) { 
   $field = explode (",", htmlentities (trim ($record))); 
   echo " <option value=\"$field[0]\">$field[1]</option>\n"; 
}

If you call fgets() with one parameter then, it keeps reading until it finds newline character or EOF (whichever comes first). Make sure you format coursesfinal.txt such as:

Animation Film Design,AFD-250
6 Digital Sculpture,DS-410
4 History of Animation,HA-240,
6 Visual Effects,VE-298

and then you can

while ($record = fgets ($DB) ) { 
   $field = explode (",", htmlentities (trim ($record))); 
   echo " <option value=\"$field[1]\">$field[0] $field[1]</option>\n"; 
}

Note that explode() with coma delimiter will split string "Animation Film Design,AFD-250" into array

$field[0] == "Animation Film Design"
$field[1] == "AFD-250"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM