Sunday, 27 October 2013

Keep checkbox checked after submit refresh in PHP

Below code maintain checkbox value when user click preview button


<?php
    if (isset($_POST['number']))
    {
        foreach ($_POST['number'] as $selectednumber)
            $selected[$selectednumber] = "checked";
    }
?>

<form action="" method="post">
    <input type="checkbox" name="number[]" <?php echo $selected['one'] ?> value="one" />one<br />
    <input type="checkbox" name="number[]" <?php echo $selected['two'] ?> value="two" />two<br />
    <input type="checkbox" name="number[]" <?php echo $selected['three'] ?> value="three" />three<br />
    <input type="checkbox" name="number[]" <?php echo $selected['four'] ?> value="four" />four<br />

<input type="submit" name="Submit" value="Preview" />
</form>