0

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.

2 Answers 2

3

@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" '; } ?>/>
Sign up to request clarification or add additional context in comments.

2 Comments

Yep, you'll need checked='checked' or checked=''. Right now you're just echoing checked.
works a treat! thank you so much. it is amazing how such a small thing can just slip my mind!
0

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.

2 Comments

that is a good point, i did design it that way because by default the two boxes are not checked. However as your bring this up it brings up the option of if they do select both boxes. But then again if both are checked then each function will run interdependently anyway.
Seems a bit odd. Really should be an if/else if/else if/else structure too. If both are set, then do both of them, else if the tv is set, run tv, else if movie is set, run movie, else "Please select a search method". Then have both checked by default, which is the exact same as having them both unchecked in the current method. Of course, you'd have to set them to false rather than unsetting them when the boxes aren't checked, and then check whether they === true or false rather than isset in order to do that.

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.