0

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");
                if(isset($_REQUEST["csak_akcios"]))
                {
                    $default = $_REQUEST["csak_akcios"];
                }
                else
                {
                    $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>';

2 Answers 2

1

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

else
{
    $default = array();
}

And replace the following two lines

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

with

$checked = (isset($default) && $default == '1') ? 'checked="checked"' : '';
echo '<div class="checkbox padding_left_10 filter_div">
        <label>
            <input type="checkbox"  name="csak_akcios" value="1" '.$checked.' />
                Csak az akciós termékek mutatása ('.$termekdarab["counted"].')'.'
        </label>
    </div>';
Sign up to request clarification or add additional context in comments.

Comments

1

something like this: (if you using POST in your form)

<label><input type="checkbox" name="myName" value="myValue" <?php if(isset($_POST['myName'])) echo "checked='checked'"; ?> > This is checkbox</label>

$_POST['myName'] binds to name="myName".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.