1

Using PHP and sessions, how can I check a checkbox the first time the page is loaded?

Here is a minimal sample of my code:

index.php:

<?php session_start(); $_SESSION['foo'] = $_POST['foo']; ?> <!doctype html> <html> <body> <form method='POST'> <input type="checkbox" id="checkFoo" name="foo" <?php echo (isset($_POST['foo']) && $_POST['foo'] === 'on') ? 'checked' : ''; ?>> <label for="checkFoo">checkbox</label> <button type="submit">Submit</button> </form> <?php include 'print.php'; ?> </body> </html> 

print.php:

<?php session_start(); echo $_SESSION['foo'].' '; ?> 

Apart from the default value, this code works exactly as I intend:

  • When the user checks the checkbox and clicks submit, it prints 'on'
  • When the user unchecks the checkbox and clicks submit, it prints nothing
  • Clicking the submit button does not change the value of the checkbox

How can I change the default (startup) value of the checkbox to 'on', without changing the above behavior?

Edit: With the accepted answer, everything was working as I expected, except on the first load of the page - the checkbox was selected but the value printed out in code was null. I got it working with this:

index.php:

<?php session_start(); $_SESSION['submit'] = $_POST['submit'] ?? null; $_SESSION['foo'] = $_POST['foo'] ?? ''; ?> <!doctype html> <html> <body> <form method='POST'> <input type="checkbox" id="checkFoo" value="on" name="foo" <?php echo ((!isset($_POST['submit']) && $_SESSION['foo'] != 'off') || ($_SESSION['foo'] === 'on')) ? 'checked' : ''; ?>> <label for="checkFoo">checkbox</label> <button type="submit" name="submit">Submit</button> </form> <?php include 'print.php'; ?> </body> </html> 

print.php:

<?php if(!isset($_SESSION)) session_start(); $foo = ((!isset($_SESSION['submit']) && $_SESSION['foo'] != 'off') || ($_SESSION['foo'] === 'on')) ? true : false; echo 'foo='.$foo.'<br>'; ?> 
2
  • Maybe just by reversing the condition. checked by default, and unchecked if foo equals off. Commented Jun 30, 2022 at 6:09
  • Foo is never euqal to 'off', it's either 'on' or '' (null). When I set checked by default, then the box gets set to checked every time the user clicks submit, even if the user unchecks the box. Commented Jun 30, 2022 at 6:46

2 Answers 2

2

When the page will be first loaded, the $_POST['submit'] would not be set as the page would be accessed using a GET Request.

So Modify the files: index.php

<?php session_start(); $_SESSION['foo'] = $_POST['foo'] ?? ''; ?> <!doctype html> <html> <body> <form method='POST'> <input type="checkbox" id="checkFoo" value="on" name="foo" <?php echo ((!isset($_POST['submit']) && $_SESSION['foo'] != 'off') || ($_SESSION['foo'] === 'on')) ? 'checked' : ''; ?>> <label for="checkFoo">checkbox</label> <button type="submit" name="submit">Submit</button> </form> <?php include 'print.php'; ?> </body> </html> 

print.php

<?php if(!isset($_SESSION)) { session_start(); } if(isset($_SESSION['foo'])) { echo $_SESSION['foo'].' '; } ?> 
Sign up to request clarification or add additional context in comments.

Comments

0

This is a little verbose, but it's good practice to use a default value, then check if something exists before setting a variable to it.

Something like this should work.

<?php session_start(); // default value is on $foo = "on"; // if session has a value, use it instead if(isset($_SESSION["foo"])){ $foo = $_SESSION["foo"]; } // if post has a value, use it instead and set the session value if(isset($_POST["foo"])){ $foo = $_POST["foo"]; $_SESSION["foo"] = $_POST["foo"]; } $checked = $foo === "on"; 

This can be simplified in newer PHP versions (7+)

<?php session_start(); $foo = $_POST["foo"] ?? $_SESSION["foo"] ?? "on"; if(isset($_POST["foo"])){ $_SESSION["foo"] = $foo; } 

7 Comments

With this solution, at startup, the value of the session variable is set to 'on', but the checkbox is not checked. Thereafter, every time you hit submit, the session variable has a value of 'on', regardless of whether or not the checkbox is checked.
Add a hidden input field before the checkbox with the value "off" with the same name. That's a workaround. Alternatively just change the initial value of $foo to $foo === "" and change the $checked logic to $checked = $foo === "on" || $foo === ""
You'll keep running into the same issue unless you specify another default value other than an empty string (since that's what is posted with the empty checkbox).
The code that you posted did not work for reasons I described. The accepted answer fixed the problem, with one glitch which I fixed in the edit to my original post.
In the "simplified" version, it does in fact set the session variable to "on", but not in the original one. It only sets the session variable when they post field exists. Aside from that, the code is much more readable and easier to maintain.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.