Skip to content
PHP Logo

What is the best practice for incorporating form options into a URL for <audio> playback in PHP?

When incorporating form options into a URL for <audio> playback in PHP, the best practice is to use a GET request to pass the selected option as a parameter in the URL. This way, the PHP script can retrieve the selected option from the URL and use it to determine which audio file to play.

php
<?php
// Retrieve the selected option from the URL
$selectedOption = $_GET['option'];

// Define an array mapping options to audio files
$audioFiles = [
    'option1' => 'audio1.mp3',
    'option2' => 'audio2.mp3',
    'option3' => 'audio3.mp3'
];

// Check if the selected option exists in the array
if (array_key_exists($selectedOption, $audioFiles)) {
    $audioFile = $audioFiles[$selectedOption];
    echo '<audio controls><source src="' . $audioFile . '" type="audio/mpeg"></audio>';
} else {
    echo 'Invalid option selected';
}
?>