How can you set up different text fields to display based on the radio button selected in PHP?
To set up different text fields to display based on the radio button selected in PHP, you can use JavaScript to show/hide the text fields dynamically. You can achieve this by creating a function that listens for changes in the radio button selection and then shows or hides the corresponding text fields accordingly.
<script>
function showTextField() {
var selectedValue = document.querySelector('input[name="radio"]:checked').value;
if(selectedValue === 'option1') {
document.getElementById('text1').style.display = 'block';
document.getElementById('text2').style.display = 'none';
} else if(selectedValue === 'option2') {
document.getElementById('text1').style.display = 'none';
document.getElementById('text2').style.display = 'block';
}
}
</script>
<form>
<input type="radio" name="radio" value="option1" onclick="showTextField()"> Option 1 <br>
<input type="radio" name="radio" value="option2" onclick="showTextField()"> Option 2 <br>
<input type="text" id="text1" style="display:none;"> Text Field 1 <br>
<input type="text" id="text2" style="display:none;"> Text Field 2 <br>
</form>
Keywords
Related Questions
- What are the potential pitfalls of not thoroughly searching for solutions before asking for help in a PHP forum?
- How can the use of nested sets or XML data structures improve the efficiency and readability of PHP code for menu generation?
- How can the performance of a dropdown box in PHP be optimized when dealing with a high volume of data?