Parkolo11 Parkolo11 - 2 years ago 100
PHP Question

Keep a checkbox checked after submit

After i submit and the page reloads, how can i keep this checkbox checked? If it was checked by the user.

In the $checked variable, i dont need in_array, but i dont know, that what i need.

echo '<div class="sidebar_szuro_div">
<span class="sidebar_title"><i class="fa fa-caret-down" aria-hidden="true"></i> Akció</span>';
$gyartok = runsql_array("
SELECT gyarto_id, gyarto_nev FROM gyarto WHERE gyarto_id in (SELECT DISTINCT termek_gyarto FROM termek WHERE termek_id IN ( SELECT kat_kapcs_termek_id FROM `termek_katgoria_kapcsolo` WHERE kat_kapcs_kategoria_id='$kat_id' )) ORDER BY gyarto_nev ASC");
$default = $_REQUEST["csak_akcios"];
$default = array();
echo '<div class="filters_container">';

$termekdarab = runsql("SELECT COUNT(termek_id) AS counted FROM termek WHERE termek_status = 1 AND termek_akcio = 1
AND termek_id IN ( SELECT kat_kapcs_termek_id FROM `termek_katgoria_kapcsolo` WHERE kat_kapcs_kategoria_id='$kat_id' )

//$checked = in_array($gy["gyarto_id"] , $default)?' checked ':''; ????
echo '<div class="checkbox padding_left_10 filter_div"><label><input type="checkbox" name="csak_akcios" value="1">Csak az akciós termékek mutatása ('.$termekdarab["counted"].')'.'</label></div>';

echo '<div class="text-center"><button type="submit" name="filter_submit_btn" class="btn szures_btn">Mehet</button></div>';
echo '</div>';
echo '</div>';

Answer Source

First of all, remove the following else block, don't make $default an array if the checkbox is not checked.

    $default = array();

And replace the following two lines

//$checked = in_array($gy["gyarto_id"] ...
echo '<div class="checkbox padding_left_10 ...


$checked = (isset($default) && $default == '1') ? 'checked="checked"' : '';
echo '<div class="checkbox padding_left_10 filter_div">
            <input type="checkbox"  name="csak_akcios" value="1" '.$checked.' />
                Csak az akciós termékek mutatása ('.$termekdarab["counted"].')'.'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download