Asked  7 Months ago    Answers:  5   Viewed   35 times

How to read if a checkbox is checked in PHP?



If your HTML page looks like this:

<input type="checkbox" name="test" value="value1">

After submitting the form you can check it with:



if ($_POST['test'] == 'value1') ...
Tuesday, June 1, 2021
answered 7 Months ago

I believe your real problem is that you have two separate forms. Your checkbox is in one form, your submit button is in a second form. I believe they both need to be in the same form. Otherwise your checkbox state is never returned, regardless of it's state.

In your view, try replacing the form markup you provided with this:

<form data-toggle="validator" data-disable="false" role="form" action="/admin/role/add" method="post">
    <div class="checkbox checkbox-success">
        <input name="test" id="test" type="checkbox" value="test">
        <label for="test" style="padding-left: 15px!important;">test</label>
    {{ csrf_field() }}
    <div class="form-group pull-right">
        <button type="submit" class="btn btn-danger">Submit</button>
Wednesday, March 31, 2021
answered 9 Months ago

Add a hidden input for the checkbox with a different ID:

<input id='testName' type='checkbox' value='Yes' name='testName'>
<input id='testNameHidden' type='hidden' value='No' name='testName'>

Before submitting the form, disable the hidden input based on the checked condition:

if(document.getElementById("testName").checked) {
    document.getElementById('testNameHidden').disabled = true;
Tuesday, June 1, 2021
answered 7 Months ago

You can do something like:

function ChckbxsCtrl($scope, $filter) {
    $scope.chkbxs = [{
        label: "Led Zeppelin",
        val: false
    }, {
        label: "Electric Light Orchestra",
        val: false
    }, {
        label: "Mark Almond",
        val: false

    $scope.$watch("chkbxs", function(n, o) {
        var trues = $filter("filter")(n, {
            val: true
        $scope.flag = trues.length;
    }, true);

And a template:

<div ng-controller="ChckbxsCtrl">
    <div ng-repeat="chk in chkbxs">
        <input type="checkbox" ng-model="chk.val" />
    <div ng-show="flag">I'm ON when band choosed</div>


UPDATE: Or you can go little bit different way , without using $scope's $watch() method, like:

$scope.bandChoosed = function() {
    var trues = $filter("filter")($scope.chkbxs, {
        val: true
    return trues.length;

And in a template do:

<div ng-show="bandChoosed()">I'm ON when band choosed</div>


Friday, June 18, 2021
answered 6 Months ago

One way to solve this is to reinforce your intentions by expliciting formmethod="post", like this:

<button type="submit" formmethod="post" formaction="add_user.php">Submit</button>
Friday, July 30, 2021
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :