How to keep the radio button selected if the page is refreshed How to keep the radio button selected if the page is refreshed codeigniter codeigniter

How to keep the radio button selected if the page is refreshed


It is very easy with PHP. You can use isset() inside the HTML tag for the condition to check or uncheck.

isset($a[0]) ? 'checked' : ''

Use this in the radio button

<input type="radio" name="jawaban<?php echo $row->id_quiz; ?>[]" value="<?=$a[0]?>" <?= isset($a[0]) ? 'checked' : ''?> >


You can maintain your radio values into cookies. But maintaining cookies for radio values is very difficult or you can use local storage.

Some references are given below to use cookies and local storage.

1.https://www.w3schools.com/js/js_cookies.asp

2.https://www.w3schools.com/JSREF/prop_win_localstorage.asp


You can use LocalStorage:

window.localStorage.setItem('user', 'jawaban'); //Set or savewindow.localStorage.getItem('user'); //get back the saved value

enter image description here