How to Keep Checkbox Checked After form Submit in PHP

How to Keep Checkbox Checked After form Submit in PHP

In this blog, you will learn how to keep the checkbox checked after the form submit. When we use validation in the form then there is a need to keep the checkbox checked after the form submit.

If we left any input empty then our remaining input should be filled with the previous value that the user filled, if our form got blanked then the user irritates to fill up again so for user experience we need our input should be filled up with the value after submit.

Let’s see the solution how to keep the checkbox checked after submitting:


$nameErr = $emailErr = $mobileErr = $priceErr=" ";
$name = $email = $mobile = $price="";
if (isset($_POST["submit"])) {
 if (empty($_POST["name"])) {
    $nameErr = "Name is required";
  } else {
    $name = $_POST["name"];
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z-' ]*$/",$name)) {
      $nameErr = "Only letters and white space allowed";
  if (empty($_POST["email"])) {
    $emailErr = "Email is required";
  } else {
    $email = $_POST["email"];
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      $emailErr = "Invalid email format";
  if (empty($_POST["mobile"])) {
    $mobileErr = "Mobile is required";
  } else {
    $mobile = $_POST["mobile"];
   if (!preg_match('/^[0-9]{10}+$/', $mobile)) {
      $mobileErr = "Only digits allowed";
    if (empty($_POST["price"])) {
    $priceErr = "price is required";
  } else {
    $price = $_POST["price"];
<!DOCTYPE html>
<html lang="en">
    <title>Form Validation</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="[email protected]/dist/css/bootstrap.min.css">
    <script src="[email protected]/dist/jquery.slim.min.js"></script>
    <script src="[email protected]/dist/umd/popper.min.js"></script>
    <script src="[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<body style="background-color: #8affaf;">
    <div class="container">
        <h2 class="text-center mt-5 text-primary">CONTACT US</h2>
        <form method="post" class="needs-validation" enctype="multipart/form-data">
   <div class="form-group">
                <label for="uname">NAME:</label>
                <input type="text" class="form-control"  placeholder="Enter username" name="name" value="<?php if(isset($_POST['name'])){echo $_POST['name'];} ?>" >
            <span class="error">* <?php echo $nameErr;?></span>
            <div class="form-group">
                <label for="uname">EMAIL:</label>
                <input type="email" class="form-control"  placeholder="Enter Email" name="email" value="<?php if(isset($_POST['email'])){echo $_POST['email'];} ?>">
              <span class="error">* <?php echo $emailErr;?></span>
            <div class="form-group">
                <label for="uname">MOBILE NUMBER:</label>
                <input type="text" class="form-control"  placeholder="Enter Mobile no" name="mobile" value="<?php if(isset($_POST['mobile'])){echo $_POST['mobile'];} ?>" >
               <span class="error">* <?php echo $mobileErr;?></span>
 <div class="form-group">
 <input type="checkbox"  name="price[]" value="1000" <?php if(isset($_POST['price'])){if(in_array("1000",$_POST['price'])){echo 'checked';}} ?>> 1000
 <input type="checkbox"  name="price[]" value="2000" <?php if(isset($_POST['price'])){if(in_array("2000",$_POST['price'])){echo 'checked';}} ?>> 2000
<input type="checkbox"  name="price[]" value="3000" <?php if(isset($_POST['price'])){if(in_array("3000",$_POST['price'])){echo 'checked';}} ?>> 3000
<p><span class="error"> <?php echo $priceErr;?></span></p>
   <button type="submit" name="submit" class="btn btn-primary p-3 btn-block my-5">Submit</button>