I have made the following form in HTML:
<form method="post" action="survey.php">
<div>
<label class="prompt">Favorite CS courses:</label>
<label>
<input type="checkbox" name="courses[]" value="course-web">Web Development
</label>
<label>
<input type="checkbox" name="courses[]" value="course-net">Networking
</label>
<label>
<input type="checkbox" name="courses[]" value="course-gui">GUI
</label>
<label>
<input type="checkbox" name="courses[]" value="course-oop">OOP
</label>
</div>
<div>
<input type="submit" value="Submit Survey">
</div>
</form>
When the user submits the form, it submits to a PHP file that I have. In the file, I have the following variable assignment to receive the courses[] from HTML:
$courses = $_POST["courses"];
In my PHP code, I have the following foreach loop so that I can print out the values from the array:
// output the user's favorite courses
echo "<p>Favorite Courses:</p> <ul>";
foreach ($courses as $course)
{
echo "<li><strong>$course</strong></li>";
}
echo "</ul>";
The problem is that when I ouput the courses, it comes out like this:
Favorite Courses:
course-web
course-net
course-gui
course-oop
I would like to change my code so that it outputs it like this:
Favorite Courses:
Web Development
Networking
GUI
OOP
How should I go about doing this without changing any of the HTML code.
The best solution that I have come up with for this problem is the following code, but I feel like it could be improved a lot:
// output the user's favorite courses
echo "<p>Favorite Courses:</p> <ul>";
foreach ($courses as $course)
{
if ($course == 'course-web')
{
echo "<li><strong>Web Development</strong></li>";
}
if ($course == 'course-net')
{
echo "<li><strong>Networking</strong></li>";
}
if ($course == 'course-gui')
{
echo "<li><strong>GUI</strong></li>";
}
if ($course == 'course-oop')
{
echo "<li><strong>OOP</strong></li>";
}
}
echo "</ul>";
valueattributes, so unless your PHP code has access to the text from another source, there's no way you can do this.