Checked flag status by default

I have a page with a search form on it and a table with the search results below. In the search form, I have the checkbox "Search in this category". What I do to check it by default:

if(!isset($_SESSION['inthiscat'])){
    $_SESSION['inthiscat'] = 'on' ;
    $checked = 'checked';
}
$_GET['inthiscat'] = $_SESSION['inthiscat'];

cell code: INPUT type = "checkbox" name = "inthiscat" <?=$checked?>. Link to next page of index.php results? Inthiscat = $ _ GET ['inthiscat']. So the problem is that I will uncheck the box next to “Search this category,” which is still checked when I go to the next page of results. How to fix it and what am I doing wrong? Of course, the entry level session.

+3
source share
2 answers

-, SESSION? , , GET , .

, , - GET:

<?php
session_start();
//......
//......
$checked='checked';
if(isset($_REQUEST['inthiscat'])) {
   // Form input and url GET parameters take precedence
   if($_REQUEST['inthiscat'] != 'checked') { $checked = ''; };
} else if(isset($_SESSION['inthiscat'])) { 
   // Next, use session variable if it exists
   if($_SESSION['inthiscat'] != 'checked') { $checked = ''; };
};
$_SESSION['inthiscat']=$checked;
?>

:

1) GET .

2) , FORM.

3) IMO, SESSION, GET URL-. GET URL-.

+1

: $_SESSION['inthiscat'] - ?

:

if (isset($_GET['inthiscat'])) {
    $_SESSION['inthiscat'] = $_GET['inthiscat'];
}
if (!isset($_SESSION['inthiscat'])) {
    $checked = 'checked';
} else {
    if ($_SESSION['inthiscat'] == 'on') {
       $checked = 'checked';
    } else {
       $cheked = '';
    }
}

HTML: <INPUT type="checkbox" name="inthiscat" checked="<?=$checked?>" value="on" />

, :

  • GET , , ( 'on' '') ;
  • ( , GET ), , , ;
  • inthiscat , , GET . , on, ; .
0

Source: https://habr.com/ru/post/1786848/


All Articles