How can radio buttons be preselected after submission in a PHP form?
To preselect radio buttons after submission in a PHP form, you can use the `checked` attribute in the input tag and set it based on the value received from the form submission. You can check the submitted value against the predefined values for each radio button and add the `checked` attribute to the corresponding input tag.
<form method="post">
<input type="radio" name="option" value="option1" <?php if(isset($_POST['option']) && $_POST['option'] == 'option1') echo 'checked'; ?>> Option 1
<input type="radio" name="option" value="option2" <?php if(isset($_POST['option']) && $_POST['option'] == 'option2') echo 'checked'; ?>> Option 2
<input type="submit" value="Submit">
</form>
Keywords
Related Questions
- How can one ensure the data integrity and security when inserting records into a MySQL database using PHP?
- What are common challenges when using PHP on mobile devices like smartphones or tablets?
- What potential issues can arise when using UNION and GROUP in MySQL queries for complex database queries like chat programs?