hi im attempting to keep a group of tick boxes ticked after i refresh the browser in been through the forums trying different solutions but anything ive tried doesnt work if anyone has a solution to this be much grateful

index.php

<?php
session_start();
include ('connect.php');
?>


<div class="main">
            <h2>PHP: Get Values of Multiple Checked Checkboxes</h2><hr/>
            <form action="php_checkbox.php" method="post">
            <label class="heading">Select Your Technical Exposure:</label><br/><br/>
            <input type="checkbox" name="check_list[]" value="C/C++"><label>C/C++</label><br/>
            <input type="checkbox" name="check_list[]" value="Java"><label>Java</label><br/>
            <input type="checkbox" name="check_list[]" value="PHP"><label>PHP</label><br/>
            <input type="checkbox" name="check_list[]" value="HTML/CSS"><label>HTML/CSS</label><br/>
            <input type="checkbox" name="check_list[]" value="UNIX/LINUX"><label>UNIX/LINUX</label><br/><br/>
            <input type="submit" name="submit" Value="Submit"/>
            <!-----Including PHP Script----->
            <?php include 'checkbox_value.php';?>
            </form>
        </div>

processing form

<?php
session_start();
include ('connect.php');
?>
<?php
    if(isset($_POST['submit'])){
    if(!empty($_POST['check_list'])) {

    //Counting number of checked checkboxes 
    $checked_count = count($_POST['check_list']);

    echo "You have selected following ".$checked_count." option(s): <br/>";

    //Loop to store and display values of individual checked checkbox
        foreach($_POST['check_list'] as $selected) {
                echo "<p>".$selected ."</p>"; 
        }
    echo "<br/><b>Note :</b> <span>Similarily, You Can Also Perform CRUD Operations using These Selected Values.</span>";    
    }
    else{
    echo "<b>Please Select Atleast One Option.</b>";
    }
    }
?>

ty jan

Dani AI

Generated

Good catch on the warning, . It happens because $hobbies is null the first time the page loads, so in_array() gets a non-array. Bigger picture: to keep boxes ticked after refresh, persist the selection somewhere (session, cookie, DB, or localStorage) and re-check them when rendering. Also, as hinted, do your processing before output and make the result drive the form.

Here is a small, self‑contained pattern that uses PRG (Post-Redirect-Get) + $_SESSION so the choices survive refresh without duplicate form submits. It also validates against an allowlist and avoids notices by casting to an array.

<?php
session_start();

// Allowlist for safety
$allowed = ['Go','JavaScript','Python','SQL','Linux'];

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $posted = (array)($_POST['skills'] ?? []);
    $_SESSION['skills'] = array_values(array_intersect($allowed, $posted));
    header('Location: ' . $_SERVER['PHP_SELF']);
    exit;
}

$selected = $_SESSION['skills'] ?? [];

function checked_attr(string $value, array $selected): string {
    return in_array($value, $selected, true) ? ' checked' : '';
}
?>
<form method="post">
  <?php foreach ($allowed as $opt): ?>
    <label>
      <input type="checkbox"
             name="skills[]"
             value="<?php echo htmlspecialchars($opt, ENT_QUOTES); ?>"
             <?php echo checked_attr($opt, $selected); ?>>
      <?php echo htmlspecialchars($opt); ?>
    </label><br>
  <?php endforeach; ?>
  <button type="submit" name="submit" value="1">Save</button>
</form>

Tips:

  • First load: default to [] with (array)($_POST['hobbies'] ?? []) to prevent the in_array() warning you saw.
  • Use the third param of in_array() (true) for strict checks.
  • If you prefer client-side only, store selected values in localStorage on change and re-check them on DOMContentLoaded.

Recommended Answers

All 8 Replies

after googling i have done the following but have encountered a error see below:
Warning: in_array() expects parameter 2 to be array, null given in /home/jktempla/public_html/sample/index.php on line 19

Playing

<input type="checkbox" name="hobbies[]" value="Playing" <?php echo (in_array('Playing',$hobbies)) ?'checked':'' ?> ><label>Playing</label><br/>
 <input type="checkbox" name="hobbies[]" value="Coding" <?php echo (in_array('Coding',$hobbies)) ?'checked':'' ?> ><label>Coding</label><br/>
 <input type="checkbox" name="hobbies[]" value="Reading" <?php echo (in_array('Reading',$hobbies)) ?'checked':'' ?> ><label>Reading</label><br/>
 <input type="checkbox" name="hobbies[]" value="Hacking" <?php echo (in_array('Hacking',$hobbies)) ?'checked':'' ?> ><label>Hacking</label><br/>
 <input type="checkbox" name="hobbies[]" value="Playing" <?php echo (in_array('Sleeping',$hobbies)) ?'checked':'' ?> ><label>Sleeping</label>

ive check all values and names correct can anyone tell me what im missing

much appreciated

Heres the code hun

<?php

 if(!empty($_POST['hobbies'])) {

 if(empty($_POST['hobbies']) || count($_POST['hobbies']) < 2)
{
    $hobbies_error = "Please select at least 2 items for your hobbies";
    $error=true;
}
$hobbies = $_POST['hobbies'];

 //Loop through hobbies array to fetch individual hobby so that we can use them
 echo "<h2> You have selected: </h2>";
 foreach($_POST['hobbies'] as $hobby) {
 echo "<p>".$hobby ."</p>"; //Print all the hobbies

/* Alert hobbies using JS
$show = "<p>".$hobby ."</p>";
echo "<script type='text/javascript'>alert(\"Your Hobbies are: '$show'\");</script>";

 */
 }
 }
 else{
 echo "<b>Please Select at least One Hobby.</b>";
 }
 }
?>

Can't see where $hobby or $hobbies array is declared. What line is that on?

i dont think its one got the script from search engines hun ill sort it now x

sorted that but now when i select any items they are not echoing through x

sorted now :)

ty all for your help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.