medoix
medoix

Reputation: 1179

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.

Upvotes: 0

Views: 1032

Answers (2)

Phoenix
Phoenix

Reputation: 4536

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.

Upvotes: 0

stealthyninja
stealthyninja

Reputation: 10371

@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" '; } ?>/>

Upvotes: 3

Related Questions