Keep checkbox checked (or) unchecked

Discuss coding issues, and scripts related to PHP and MySQL.
User avatar
Posts: 223
Location: Holland Rotterdam

Keep checkbox checked (or) unchecked

I`m using XAMP server + your script Update - Delete Data Using PHP

I have a checkbox but it does not stay checked
or unchecked after refreshing the page

this is my code for the checkbox

Code: Select all

<input  id="dating" 
        <?php echo $dating== "1" ? " checked" : ""; ?> >
<label for="switch"></label>

Try this:

Code: Select all

<input id='dating' class='switch' type='checkbox' name='dating' value='1' <?php echo (isset($_REQUEST['dating']))? "checked='checked'":'';?> />
- The checkbox remains checked or unchecked after submiting the form (indicated via POST method), but if the page is opened again without submit, the checkbox will be unchecked because there is no form data.

Dear admin thanks for supporting

But.. Is their another possibility what works 100% when i open the page again

Else the members have to check every time the checkbox.
Or do you have a better idea to do it ?

You can put in session the status of that checkbox when form is submited, then use it from session.

Code: Select all

  $_SESSION['cbx_dating'] = isset($_REQUEST['dating'])? "checked='checked'":'';
<input id='dating' class='switch' type='checkbox' name='dating' value='1' <?php echo isset($_SESSION['cbx_dating'])? $_SESSION['cbx_dating']:'';?> />
- 'a_form_field' is the name of anoher field in that form.

With starting session at the beginning of your script:

Code: Select all

if(!isset($_SESSION)) session_start();

Similar Topics