I have a simple form with some input, 2 radio buttons and two different hidden divs. The divs are shown depending on which button the user clicks. Lets assume that the user chooses one of the radio button but that there is an error in one of the input of the form. When i click the submit button and the form loads again showing the error I want that the previous choosen radio button is already checked. I achieved this with the php code in the input code. Now my problem is that when the form loads again the radio button is checked but the hidden div that needs to be shown stays hidden. How can I solve this issue? Thank you very much for your answers.
HTML CODE:
<form method="post">
<input type="radio" name="button" id="button1" value="1" <?php if($_SERVER['REQUEST_METHOD'] == 'POST'){if(empty($error) && $button=="1"){ echo 'checked="checked"'; }} ?> />
<label>Button 1</label><br />
<input type="radio" name="button" id="button2" value="2" <?php if($_SERVER['REQUEST_METHOD'] == 'POST'){if(empty($error) && $button=="2"){ echo 'checked="checked"'; }} ?> />
<label>Button 1</label><br />
<div id="one">Div one</div>
<div id="two">Div two</div>
<input type="submit" value="submit" />
</form>
JQUERY CODE:
$('#button1').click(function(){
if($(this).is(':checked')){
$('#one').show();
$('#two').hide();
}
});
$('#button2').click(function(){
if($(this).is(':checked')){
$('#one').hide();
$('#two').show();
}
});
PHP CODE:
$error = array();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (!empty($_POST["button"])){
$button = $_POST["button"];
}
if (empty($_POST["button"])){
$error []= "Choose one option";
}
}