Get the solution ↓↓↓
Solved my issue and adding some code which I've used for.
For my radio buttons added id's and then added a .js function which checking is radio button checked, if it's true - does the trick, if it's not - does something else.
In this code:
<label><input type="radio" name="check" onclick="show1();" value="f" <?php if($_POST['check'] == 'f') { print ' checked="checked"'; } ?> > Option nr. 1</label>
<label><input type="radio" name="check" onclick="show2();" value="sl" <?php if($_POST['check'] == 'sl') { print ' checked="checked"'; } ?> >Option nr. 2</label>
<label><input type="radio" name="check" onclick="show3();" value="s" <?php if($_POST['check'] == 's') { print ' checked="checked"'; } ?> >Option nr. 3</label>
I changed to:
<label><input type="radio" id="one" name="check" onclick="show1();" value="f" <?php if($_POST['check'] == 'f') { print ' checked="checked"'; } ?> > Option nr. 1</label>
<label><input type="radio" id="two" name="check" onclick="show2();" value="sl" <?php if($_POST['check'] == 'sl') { print ' checked="checked"'; } ?> >Option nr. 2</label>
<label><input type="radio" id="three" name="check" onclick="show3();" value="s" <?php if($_POST['check'] == 's') { print ' checked="checked"'; } ?> >Option nr. 3</label>
Then added new function with checking by selected radio button:
if(document.getElementById('one').checked) {
document.getElementById('div1').style.display ='block';
document.getElementById('div2').style.display ='none';
document.getElementById('div3').style.display ='none';
}else if(document.getElementById('two').checked) {
document.getElementById('div1').style.display ='none';
document.getElementById('div2').style.display ='block';
document.getElementById('div3').style.display ='none';
} else if(document.getElementById('three').checked) {
document.getElementById('div1').style.display ='none';
document.getElementById('div2').style.display ='none';
document.getElementById('div3').style.display ='block';
}
Without this js function it was only checking radio button, but shows only first field until you clicking any other radio button. This function checks is any of them checked, if it's true = some kind function.
P.S.<?php if($_POST['name'] == 'value') { print ' checked="checked"'; } ?> is necessary in radio button parameters.
try this,
on your code
<label>
<input type="radio" name="check" onclick="show1();" value="f" <?php if($_POST['check'] == 'f') { print ' checked="checked"'; } ?> > Option nr. 1
</label>
<label>
<input type="radio" name="check" onclick="show2();" value="sl" <?php if($_POST['check'] == 'sl') { print ' checked="checked"'; } ?> >Option nr. 2
</label>
<label>
<input type="radio" name="check" onclick="show3();" value="s" <?php if($_POST['check'] == 's') { print ' checked="checked"'; } ?> >Option nr. 3
</label>
change to
<label>
<input type="radio" name="check" onclick="show1();" value="f" <?=(isset($_POST['check']) && $_POST['check'] == 'f')?'checked="checked"':"" ?> > Option nr. 1
</label>
<label>
<input type="radio" name="check" onclick="show2();" value="sl" <?=(isset($_POST['check']) && $_POST['check'] == 'sl')?'checked="checked"':"" ?> >Option nr. 2
</label>
<label>
<input type="radio" name="check" onclick="show3();" value="s" <?=(isset($_POST['check']) && $_POST['check'] == 's')?'checked="checked"':"" ?> >Option nr. 3
</label>
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.