PHP $_POST function x() in echo

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.

Answer

Solution:

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>

Source