What potential pitfalls should be avoided when using PHP to manipulate existing radio buttons based on form submissions?
When manipulating existing radio buttons based on form submissions in PHP, it is important to avoid directly outputting user input without proper validation and sanitization. This can lead to security vulnerabilities such as cross-site scripting (XSS) attacks. To prevent this, always validate and sanitize user input before using it to manipulate radio buttons.
// Validate and sanitize user input before manipulating radio buttons
$selectedOption = isset($_POST['radio_option']) ? htmlspecialchars($_POST['radio_option']) : '';
// Use the sanitized input to manipulate radio buttons
echo '<input type="radio" name="radio_option" value="option1" ' . ($selectedOption == 'option1' ? 'checked' : '') . '> Option 1';
echo '<input type="radio" name="radio_option" value="option2" ' . ($selectedOption == 'option2' ? 'checked' : '') . '> Option 2';
echo '<input type="radio" name="radio_option" value="option3" ' . ($selectedOption == 'option3' ? 'checked' : '') . '> Option 3';