PHP: Is the checkbox checked?

This entry is part 46 of 54 in the series PHP Tutorial

We look at a simple check box form where it asks a simple question. Is thecheck box checked?

<form action="chkbox.php" method="post">
    Do you like the song Upwhere We Belong?
    <input type="checkbox" name="like" value="yes"/>
    <input type="submit" name="submit_name" value="Submit"/>

if (isset($_POST['submit_name'])) {
    if (isset($_POST['like'])) {
        echo "<p>You like the song.</p>";
    } else {
        echo "<p>You don't like the song.</p>";

The check box form can be tried out here.

If a checkbox is checked, the $_POST[‘like’] will be set to 1.  Else, it will be set to 0.

Series Navigation<< PHP: Radio button me
PHP: Multi-Checkbox >>