| Viewed 109 times

How to check if a checkbox is checked in PHP


How to check if a checkbox is checked in PHP

1 Answer
Mohammad Qandeel
Answered
4

The checkbox is used if you want the user to select more than one item in the same category
Like choosing favorite places from a list
Suppose you have this form in index.php page 

<form method="GET">
    <label for="f-check">Jordan
        <input type="checkbox" name="fav-place[]" value="JO" id="f-check">
    </label>
    <label for="s-check">United Kingdom
        <input type="checkbox" name="fav-place[]" value="UK" id="s-check">
    </label>
    <input type="submit">
</form>

this form will send the form data to the same page with GET method 

now we will add this PHP code to the same page 

<?php
    if(isset($_GET['fav-place']))
    {
        $favPlace=$_GET['fav-place'];
        print_r($favPlace);
    }
?>

this code will check if check-box is checked and save the value in another variable  

the index.php code will look like 

<!doctype html>
<html>
  <head>
    <title>Hello, world!</title>
  </head>
  <body>
    <?php
        if(isset($_GET['fav-place']))
        {
            $favPlace=$_GET['fav-place'];
            print_r($favPlace);
        }
    ?>
    <form method="GET">
        <label for="f-check">Jordan
            <input type="checkbox" name="fav-place[]" value="JO" id="f-check">
        </label>
        <label for="s-check">United Kingdom
            <input type="checkbox" name="fav-place[]" value="UK" id="s-check">
        </label>
        <input type="submit">
    </form>
  </body>
</html>

Output

checkbox form

after checking the two box and submit 

the PHP code will print the value of the checkbox

submit check box to php