简体   繁体   中英

PHP Value for HTML Checkbox

I have a function that sets and unset's a $_SESSION variable depending on what was submitted from the form in my main page.

function if statement:

$_SESSION['search'] = true;
if($_POST['searchtv']){
    $_SESSION['searchtv'] = true;
} else {
    unset($_SESSION['searchtv']);
}
if($_POST['searchmovie']){
    $_SESSION['searchmovie'] = true;
} else {
    unset($_SESSION['searchmovie']);
}

The searchtv and searchmovie $_POST variables are set through the below checkboxes:

<input type="checkbox" name="searchmovie" value="movie" <? echo isset($_SESSION['searchmovie']) ? 'checked' : ''; ?>"/>

However the checked value always seems to be false and displays '' so no "checked" is set to display the tick in the box.

I do know that the $_SESSION variable is being set correctly however because in the same file i have another IF statement (below) that works 100%.

if(isset($_SESSION['searchtv'])){
    $database->searchTV($_GET['show'], $session->uid);
}
                                if(isset($_SESSION['searchmovie'])){
    $database->searchMovies($_GET['show'], $session->uid);
}
                                if(!isset($_SESSION['searchtv']) && !isset($_SESSION['searchmovie'])){
    $database->searchTV($_GET['show'], $session->uid);
    $database->searchMovies($_GET['show'], $session->uid);
}

If i only tick the searchtv checkbox it only runs the searchTV function and so on.. so i know it is being set and works.. just cannot get the feedback to the checkbox to say yes it was ticked when the search button was selected.

@medoix: Try changing this

<input type="checkbox" name="searchmovie" value="movie" <? echo isset($_SESSION['searchmovie']) ? 'checked' : ''; ?>"/>

to this

<input type="checkbox" name="searchmovie" value="movie" <?php if (isset($_SESSION['searchmovie'])) { echo 'checked="checked" '; } ?>/>

You realize that the portion of the code that checks $_SESSION['searchtv'] && $_SESSION['searchmovie'] near the bottom are both !isset. It'll only run if both aren't checked, rather than if they are.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM