Are there any best practices for preselecting radio buttons in PHP forms?
When preselecting radio buttons in PHP forms, you can achieve this by checking if the value of the radio button matches a specific value in the form submission data. If it matches, you can add the "checked" attribute to the radio button input tag to preselect it.
<form action="submit.php" method="post">
<input type="radio" name="gender" value="male" <?php if(isset($_POST['gender']) && $_POST['gender'] == 'male') echo 'checked'; ?>> Male
<input type="radio" name="gender" value="female" <?php if(isset($_POST['gender']) && $_POST['gender'] == 'female') echo 'checked'; ?>> Female
<input type="submit" value="Submit">
</form>
Keywords
Related Questions
- What are the best practices for iterating through multidimensional arrays in PHP to avoid errors or inefficiencies?
- What are some common syntax errors to watch out for when using default values for function parameters in PHP?
- What potential issue could arise when setting the Content-Type header to 'image/jpeg' in PHP?