What are best practices for handling form submissions with radio buttons in PHP?
When handling form submissions with radio buttons in PHP, it is important to validate the input to ensure that only one radio button is selected. This can be done by checking if the radio button value is set and matches the expected value. Additionally, sanitize the input to prevent any malicious code injection.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if(isset($_POST['radio_button']) && ($_POST['radio_button'] == 'option1' || $_POST['radio_button'] == 'option2' || $_POST['radio_button'] == 'option3')) {
// Process the form submission
$selected_option = $_POST['radio_button'];
// Sanitize the input if needed
$sanitized_option = filter_var($selected_option, FILTER_SANITIZE_STRING);
} else {
// Handle invalid input
echo "Invalid selection";
}
}
?>
Keywords
Related Questions
- What are the security considerations when sending files from an external server to a network printer using PHP?
- How important is it to consider the interaction between PHP and JavaScript when developing web applications?
- What are the best practices for developing PHP applications on production servers like Strato?