What are the best practices for handling form elements like select boxes in PHP?

When handling form elements like select boxes in PHP, it is important to properly sanitize and validate user input to prevent security vulnerabilities such as SQL injection or XSS attacks. One common practice is to use the $_POST superglobal to retrieve the selected value from the form submission and then validate it before processing or storing it in a database.

// Retrieve the selected value from the select box
$selectedOption = isset($_POST['select_box']) ? $_POST['select_box'] : '';

// Validate the selected option (e.g. check if it is a valid option)
$validOptions = ['option1', 'option2', 'option3'];
if (!in_array($selectedOption, $validOptions)) {
    // Handle invalid option (e.g. display an error message)
    echo "Invalid option selected";
} else {
    // Process the selected option (e.g. save it to a database)
    // Your code here
}