1

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 &nbsp;
            </label>
            <label>
                <input type="checkbox" name="courses[]" value="course-net">Networking &nbsp;
            </label>
            <label>
                <input type="checkbox" name="courses[]" value="course-gui">GUI &nbsp;
            </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>";
1
  • 3
    The option texts are not transmitted to PHP, only the value attributes, so unless your PHP code has access to the text from another source, there's no way you can do this. Commented Nov 13, 2020 at 21:27

1 Answer 1

2

Create an array that maps values submitted by the form to human-readable names:

$names = [
    'course-web' => 'Web Development',
    'course-net' => 'Networking',
    'course-gui' => 'GUI',
    'course-oop' => 'OOP',
];

Loop through the array of values received and look up the human-readable name in the array:

foreach ($courses as $course)
    if ( strlen( $name = $names[ $course ] ) ) { // Yes, assign
        echo "<li><strong>$name</strong></li>";
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.