How to get value of checked checkbox in php?

How to get value of checked checkbox in php?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

How to get value of checked checkbox in PHP?

When you have a table generated dynamically from a database query and populate it with checkboxes, retrieving those specific selections upon form submission requires understanding how HTML forms serialize data. The mechanism isn't magic; it relies entirely on the name attribute structure you assign to each input element.

In your provided code snippet, you are generating a list of items using a while loop:

$table .= "<td><input type='checkbox' class=".check."  name='cbname3[]' id='chkItems' value=".$row['id']." /></td>";

You have correctly assigned the name attribute as cbname3[]. The square brackets ([]) are critical. They tell PHP to expect an array rather than a single scalar value. When the user clicks "Send" (the submit button in your second form), PHP will parse the $_POST superglobal and populate the key cbname3 with an array containing only the IDs of the checked boxes. If no checkboxes are selected, this key might not exist at all or could contain an empty string depending on browser settings, so you must verify its existence before processing.

Retrieving Selected Values

To get the value of the checked checkboxes in your PHP logic, you need to check if the cbname3 key exists within $_POST. If it does, it will be a numeric array because your database IDs are integers (or can be treated as such).

Here is how you should structure the retrieval logic inside your form handler:

if (isset($_POST['Send'])) {
    // Check if the checkbox group was submitted and contains data
    if (isset($_POST['cbname3']) && !empty($_POST['cbname3'])) {
        $selectedIds = $_POST['cbname3'];
        
        // You now have an array of IDs like: [10, 25, 42]
        // Iterate through them to process the data
        
        foreach ($selectedIds as $id) {
            // Example: Fetch specific user details or perform an action
            echo "Processing item with ID: " . $id; 
        }
    } else {
        // Handle the case where no items were selected
        echo "No items were selected.";
    }
}

Handling the "Select All" Logic

Your JavaScript includes logic to handle a "Select All" checkbox (cbname3[] with value main). This introduces a specific edge case you must account for in PHP. When the user clicks the header checkbox, your JS sets the state of individual checkboxes, but it also toggles the parent checkbox itself.

If your JavaScript successfully updates the parent checkbox's visual state or if the browser sends the parent checkbox's value alongside the array, $_POST['cbname3'] might contain a mix of numeric IDs and the string 'main'.

You should filter out the 'main' value if you only intend to process the actual row IDs:

$selectedIds = $_POST['cbname3'];

// Filter out the 'main' selector if it exists in the array
$filteredIds = array_filter($selectedIds, function($value) {
    return $value !== 'main';
});

// Convert to a clean integer array (removes empty strings left by filter)
$cleanIds = array_map('intval', array_values($filteredIds));

Security Considerations and Legacy Code

Looking at the source material, there are significant architectural decisions that need attention if you plan to maintain or extend this application. The code uses the deprecated mysql_* functions (e.g., mysql_fetch_assoc, mysql_num_fields). These extensions were removed in PHP 5.5 and are unsupported in modern PHP versions like 8.x.

If you attempt to run this code on a current server, it will fail immediately with fatal errors regarding undefined functions. You must migrate to either MySQLi or PDO. Furthermore, the SQL construction in your snippet uses string concatenation directly into the query:

$sql = "SELECT * FROM $nameOfDbWithCustomers";
// ...
$sql .=" WHERE ";
$sql.=" ".implode(" AND ", $sql_where);

This approach is vulnerable to SQL injection if any of the variables ($name, $post, etc.) contain malicious input. While you are currently using isset() checks to default empty values to 0 or skipping them, this is a fragile pattern for filtering. In a modern implementation using PDO prepared statements, you would bind parameters directly, ensuring that user input never interferes with the SQL syntax.

Conclusion on Implementation

To successfully get the value of checked checkboxes, rely on the array naming convention (name='group'[]) and always validate isset($_POST['group']) before accessing the data. Be mindful of any "select all" logic in your JavaScript that might introduce non-numeric values into your PHP array, and ensure your underlying database interaction layer is upgraded to support modern PHP security standards.

Our network