What are some common methods for making a text field required based on a radio button selection in PHP forms?

To make a text field required based on a radio button selection in PHP forms, you can use JavaScript to dynamically add or remove the "required" attribute from the text field based on the radio button selection. This can be achieved by listening for the change event on the radio button and updating the text field's required attribute accordingly.

<form>
  <input type="radio" name="option" value="yes" id="yes"> Yes
  <input type="radio" name="option" value="no" id="no"> No
  <input type="text" name="text_field" id="text_field">
</form>

<script>
document.getElementById('yes').addEventListener('change', function() {
  document.getElementById('text_field').required = true;
});

document.getElementById('no').addEventListener('change', function() {
  document.getElementById('text_field').required = false;
});
</script>