title: I want to make my radio buttons sticky in my form in php?
body: <p>My form has some radio buttons and I wish to make them sticky so when the user goes back to the form to fill it again after getting errors, the value he previously entered should be their for radio buttons as well. Sticky form is working for all the other inputs but not radio buttons?</p>
  <p>html code</p>
  <pre><code> &lt;div class='modal fade' id='staticBackdrop' data-bs-backdrop='static' data-bs-keyboard='false' tabindex='-1' aria-labelledby='staticBackdropLabel' aria-hidden='true'&gt;
    &lt;div class='modal-dialog'&gt;
     
      &lt;div class='modal-content'&gt;
        &lt;div class='modal-header'&gt;
        &lt;form action='Add.php' method='POST' class='form'&gt;
                         &lt;h2&gt;Car Registration Form&lt;/h2&gt;
                         &lt;div class='mb-3'&gt;
  
  
                             &lt;input type='radio' name='Car' id='Toyota' value='$Models[Toyota]' /&gt;
  
                             &lt;label for='Toyota' class='form-label'&gt;Toyota&lt;/label&gt;
                             &lt;select name='Models[Toyota]' class='form-control'&gt;
                                 &lt;option value='&gt;None&lt;/option&gt;
                                 &lt;option value='Camri'&gt;Camri&lt;/option&gt;
                                 &lt;option value='Corolla'&gt;Corolla&lt;/option&gt;
                                 &lt;option value='Estima'&gt;Estima&lt;/option&gt;
                             &lt;/select&gt;&lt;br&gt;
  
                             &lt;input type='radio' name='Car' id='Nissan' value='$Models[Nissan]' /&gt;
  
                             &lt;label for='Nissan' class='form-label'&gt;Nissan&lt;/label&gt;
                             &lt;select name='Models[Nissan]' class='form-control'&gt;
                                 &lt;option value='&gt;None&lt;/option&gt;
                                 &lt;option value='Micra'&gt;Micra&lt;/option&gt;
                                 &lt;option value='roller'&gt;roller&lt;/option&gt;
                                 &lt;option value='fushe'&gt;fushe&lt;/option&gt;
                             &lt;/select&gt;&lt;br&gt;
  
                             &lt;input type='radio' name='Car' id='Mercedez' value='$Models[Mercedez]' /&gt;
  
                             &lt;label for='Mercedez' value='$Models[Mercedez]' class='form-label'&gt;Mercedez&lt;/label&gt;
                             &lt;select name='Models[Mercedez]' class='form-control'&gt;
                                 &lt;option value='&gt;None&lt;/option&gt;
                                 &lt;option value='5series'&gt;5series&lt;/option&gt;
                                 &lt;option value='x7'&gt;x7&lt;/option&gt;
                                 &lt;option value='x5'&gt;x5&lt;/option&gt;
                             &lt;/select&gt;&lt;br&gt;
  
  
  
                             &lt;label for='vehicleidentificationnumber' class='form-label'&gt;vehicle identification number (VIN)&lt;/label&gt;
                             &lt;input type='text' name='vehicleidentificationnumber' value='$vehicleidentificationnumber' class='form-control' maxlength=20 required&gt;
  
                             &lt;label for='ManufacturingYear' class='form-label'&gt;Manufacturing Year&lt;/label&gt;
                             &lt;input type='text' name='ManufacturingYear' value='$ManufacturingYear' class='form-control' maxlength=20 required&gt;
  
                             &lt;label for='Enginesize' class='form-label'&gt;Engine size&lt;/label&gt;
                             &lt;input type='text' name='Enginesize' value='$Enginesize' class='form-control' maxlength=20 required&gt;
  
                             &lt;label for='TransmissionType' class='form-label'&gt;TransmissionType&lt;/label&gt;&lt;br&gt;
                             &lt;input type='radio' name='TransmissionType' id='TransmissionType' value='$Automatic' /&gt;
                             &lt;label for='Automatic' name='Automatic' class='form-label'&gt;Automatic&lt;/label&gt;&lt;br&gt;
  
                             &lt;input type='radio' name='TransmissionType' id='TransmissionType' value='$Manual' /&gt;
                             &lt;label for='Manual' class='form-label'&gt;Manual&lt;/label&gt;&lt;br&gt;
  
                             &lt;input type='radio' name='TransmissionType' id='TransmissionType' value='$Semi-Automatic' /&gt;
                             &lt;label for='Semi-Automatic' class='form-label'&gt;Semi-Automatic&lt;/label&gt;&lt;br&gt;
  
                             &lt;label for='NoofSeats' class='form-label'&gt;No. of Seats&lt;/label&gt;
                             &lt;input type='number' name='NoofSeats' value='$NoofSeats' class='form-control' maxlength=20 required&gt;
  
                             &lt;label for='Noofdoors' class='form-label'&gt;No. of doors&lt;/label&gt;
                             &lt;input type='number' name='Noofdoors' value='$Noofdoors' class='form-control' maxlength=20 required&gt;
  
                             &lt;label for='Fueltype' class='form-label'&gt;Fuel type&lt;/label&gt;
                             &lt;input type='text' name='Fueltype' value='$Fueltype' class='form-control' maxlength=20 required&gt;
  
                             &lt;label for='Colour' class='form-label'&gt;Colour&lt;/label&gt;
                             &lt;input type='text' name='Colour' value='$Colour' class='form-control' maxlength=20 required&gt;
  
                             &lt;label for='RegistrationNumber' class='form-label'&gt;Registration Number (use Dublin registration) &lt;/label&gt;
                             &lt;input type='text' name='RegistrationNumber' value='$RegistrationNumber' class='form-control' maxlength=20 required&gt;
  
                             &lt;label for='Dateoffirstregistration' class='form-label'&gt;Date of first registration&lt;/label&gt;
                             &lt;input type='date' name='Dateoffirstregistration' value='$Dateoffirstregistration' class='form-control' maxlength=20 required&gt;
  
                         &lt;/div&gt;
  
                         &lt;button type='button' class='btn btn-secondary' data-bs-dismiss='modal'&gt;Close&lt;/button&gt;
                         &lt;button type='submit' name='insertdata' class='btn btn-primary'&gt;Add Car&lt;/button&gt;
  
                      &lt;/form&gt;
  
                  &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt; &quot;;
  </code></pre>
  <p>** php code **</p>
  <pre><code>if ($_SERVER['REQUEST_METHOD'] == 'POST') {   // checking method
      $errors = array(); // creating an errors array to record errors if there are any.
  
  
      if (empty($_POST['vehicleidentificationnumber'])) {
          $errors[] = 'vehicle identification number is required.';
      } else {
          // trim function removes whitespace and other predefined characters from both sides of a string.
          $vehicleidentificationnumber = trim($_POST['vehicleidentificationnumber']);
          // checking for the valid pattern from user
          if (!preg_match('/^[a-zA-Z1-9]{17}$/', $vehicleidentificationnumber)) {
              $errors[] = 'Invalid vehicle identification number! use only letters and numbers';
          }
      }
  
      // check if the ManufacturingYear is provided and is valid
      if (empty($_POST['ManufacturingYear'])) {
          $errors[] = 'Manufacturing Year is required.';
      } else {
          $ManufacturingYear = trim($_POST['ManufacturingYear']);
          if (!preg_match('/^[0-9]{4}$/', $ManufacturingYear)) {
              $errors[] = 'Invalid Manufacturing Year! use only year format yyyy';
          }
      }
  
      // check if the Enginesize is provided and is valid
      if (empty($_POST['Enginesize'])) {
          $errors[] = 'Enginesize is required.';
      } else {
          $Enginesize = trim($_POST['Enginesize']);
          if (!preg_match('/^[1-9.L]*$/', $Enginesize)) {
              $errors[] = 'Invalid Enginesize! use only letters/numbers.';
          }
      }
  
      // check if the Transmissiontype is provided and is valid
      if (empty($_POST['TransmissionType'])) {
          $errors[] = 'Transmission type is required.';
      } else {
          $TransmissionType = trim($_POST['TransmissionType']);
          if (!preg_match('/^[a-zA-Z]*$/', $TransmissionType)) {
              $errors[] = 'Invalid Transmission type! use only letters.';
          }
      }
  
      // check if the NoofSeats is provided and is valid
      if (empty($_POST['NoofSeats'])) {
          $errors[] = 'NoofSeats is required.';
      } else {
          $NoofSeats = trim($_POST['NoofSeats']);
          if (!preg_match('/^[1-9]*$/', $NoofSeats)) {
              $errors[] = 'Invalid NoofSeats! use only numbers';
          }
      }
  
      // check if the Noofdoors is provided and is valid
      if (empty($_POST['Noofdoors'])) {
          $errors[] = 'Noofdoors is required.';
      } else {
          $Noofdoors = trim($_POST['Noofdoors']);
          if (!preg_match('/^[1-9]*$/', $Noofdoors)) {
              $errors[] = 'Invalid Noofdoors! use only numbers.';
          }
      }
  
      // check if the Fueltype is provided and is valid
      if (empty($_POST['Fueltype'])) {
          $errors[] = 'Fueltype is required.';
      } else {
          $Fueltype = trim($_POST['Fueltype']);
          if (!preg_match('/^[a-zA-Z]*$/', $Fueltype)) {
              $errors[] = 'Invalid Fueltype! use only letters.';
          }
      }
  
      // check if the Colour is provided and is valid
      if (empty($_POST['Colour'])) {
          $errors[] = 'Colour is required.';
      } else {
          $Colour = trim($_POST['Colour']);
          if (!preg_match('/^[a-zA-Z]*$/', $Colour)) {
              $errors[] = 'Invalid Colour! use only letters.';
          }
      }
  
      // check if the RegistrationNumber is provided and is valid
      if (empty($_POST['RegistrationNumber'])) {
          $errors[] = 'RegistrationNumber is required.';
      } else {
          $RegistrationNumber = trim($_POST['RegistrationNumber']);
          if (!preg_match('/^[0-9]{3}-[D]{1}-[0-9]{5}$/', $RegistrationNumber)) {
              $errors[] = 'Invalid RegistrationNumber! use only following format 131-D-12345';
          }
      }
  
      // check if the Dateoffirstregistration is provided and is valid
      if (empty($_POST['Dateoffirstregistration'])) {
          $errors[] = 'Dateoffirstregistration is required.';
      } else {
          $Dateoffirstregistration = trim($_POST['Dateoffirstregistration']);
          if (!preg_match('/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/', $Dateoffirstregistration)) {
              $errors[] = 'Invalid Dateoffirstregistration! use only date format dd/mm/yyyy';
          }
      }
  
      if (empty($errors)) {
          require('/home/s3022041/sqlC/dbConnect.php');
  
          $Car = $_POST['Car'];
          $Models = $_POST['Models'][$Car];
          $vehicleidentificationnumber = $_POST['vehicleidentificationnumber'];
          $ManufacturingYear = $_POST['ManufacturingYear'];
          $Enginesize = $_POST['Enginesize'];
          $TransmissionType = $_POST['TransmissionType'];
          $NoofSeats = $_POST['NoofSeats'];
          $Noofdoors = $_POST['Noofdoors'];
          $Fueltype = $_POST['Fueltype'];
          $Colour = $_POST['Colour'];
          $RegistrationNumber = $_POST['RegistrationNumber'];
          $Dateoffirstregistration = $_POST['Dateoffirstregistration'];
  
  
          require('/home/s3022041/sqlC/dbConnect.php');
          $query = &quot;INSERT INTO cars (make, model, VIN, Manufacture_Year, Engine_Size, Transmission_Type, NoOfSeats, NoOfDoors, Fuel_Type, Colour, Registration_Number, DateOfRegestration) 
                      VALUES ('$Car','$Models', '$vehicleidentificationnumber','$ManufacturingYear','$Enginesize','$TransmissionType','$NoofSeats','$Noofdoors','$Fueltype','$Colour','$RegistrationNumber','$Dateoffirstregistration');&quot;;
          $query_run = mysqli_query($connection, $query);
          if ($query_run) {
              echo &quot;&lt;h2&gt;Added successfully&lt;/h2&gt;&quot;;
              echo &quot;&lt;a href='index.php'&gt;home&lt;/a&gt;&quot;;
          } else {
              echo 'Error! ' . mysqli_error($connection);
          }
          mysqli_close($db_connection); // close the database connection
  
      } else {
          echo &quot;&lt;h3 class='errorH'&gt;Error! &lt;br&gt;The following error(s) occurred:&lt;/h3&gt;&quot;;
          foreach ($errors as $msg) {
              echo &quot;&lt;div class='error'&gt;$msg &lt;/div&gt;&quot;;
          }
  </code></pre>