I want to make my radio buttons sticky in my form in php?

** So 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? **

** html code **

 <div class='modal fade' id='staticBackdrop' data-bs-backdrop='static' data-bs-keyboard='false' tabindex='-1' aria-labelledby='staticBackdropLabel' aria-hidden='true'>
  <div class='modal-dialog'>
    <div class='modal-content'>
      <div class='modal-header'>
      <form action='Add.php' method='POST' class='form'>
                       <h2>Car Registration Form</h2>
                       <div class='mb-3'>

                           <input type='radio' name='Car' id='Toyota' value='$Models[Toyota]' />

                           <label for='Toyota' class='form-label'>Toyota</label>
                           <select name='Models[Toyota]' class='form-control'>
                               <option value='>None</option>
                               <option value='Camri'>Camri</option>
                               <option value='Corolla'>Corolla</option>
                               <option value='Estima'>Estima</option>

                           <input type='radio' name='Car' id='Nissan' value='$Models[Nissan]' />

                           <label for='Nissan' class='form-label'>Nissan</label>
                           <select name='Models[Nissan]' class='form-control'>
                               <option value='>None</option>
                               <option value='Micra'>Micra</option>
                               <option value='roller'>roller</option>
                               <option value='fushe'>fushe</option>

                           <input type='radio' name='Car' id='Mercedez' value='$Models[Mercedez]' />

                           <label for='Mercedez' value='$Models[Mercedez]' class='form-label'>Mercedez</label>
                           <select name='Models[Mercedez]' class='form-control'>
                               <option value='>None</option>
                               <option value='5series'>5series</option>
                               <option value='x7'>x7</option>
                               <option value='x5'>x5</option>

                           <label for='vehicleidentificationnumber' class='form-label'>vehicle identification number (VIN)</label>
                           <input type='text' name='vehicleidentificationnumber' value='$vehicleidentificationnumber' class='form-control' maxlength=20 required>

                           <label for='ManufacturingYear' class='form-label'>Manufacturing Year</label>
                           <input type='text' name='ManufacturingYear' value='$ManufacturingYear' class='form-control' maxlength=20 required>

                           <label for='Enginesize' class='form-label'>Engine size</label>
                           <input type='text' name='Enginesize' value='$Enginesize' class='form-control' maxlength=20 required>

                           <label for='TransmissionType' class='form-label'>TransmissionType</label><br>
                           <input type='radio' name='TransmissionType' id='TransmissionType' value='$Automatic' />
                           <label for='Automatic' name='Automatic' class='form-label'>Automatic</label><br>

                           <input type='radio' name='TransmissionType' id='TransmissionType' value='$Manual' />
                           <label for='Manual' class='form-label'>Manual</label><br>

                           <input type='radio' name='TransmissionType' id='TransmissionType' value='$Semi-Automatic' />
                           <label for='Semi-Automatic' class='form-label'>Semi-Automatic</label><br>

                           <label for='NoofSeats' class='form-label'>No. of Seats</label>
                           <input type='number' name='NoofSeats' value='$NoofSeats' class='form-control' maxlength=20 required>

                           <label for='Noofdoors' class='form-label'>No. of doors</label>
                           <input type='number' name='Noofdoors' value='$Noofdoors' class='form-control' maxlength=20 required>

                           <label for='Fueltype' class='form-label'>Fuel type</label>
                           <input type='text' name='Fueltype' value='$Fueltype' class='form-control' maxlength=20 required>

                           <label for='Colour' class='form-label'>Colour</label>
                           <input type='text' name='Colour' value='$Colour' class='form-control' maxlength=20 required>

                           <label for='RegistrationNumber' class='form-label'>Registration Number (use Dublin registration) </label>
                           <input type='text' name='RegistrationNumber' value='$RegistrationNumber' class='form-control' maxlength=20 required>

                           <label for='Dateoffirstregistration' class='form-label'>Date of first registration</label>
                           <input type='date' name='Dateoffirstregistration' value='$Dateoffirstregistration' class='form-control' maxlength=20 required>


                       <button type='button' class='btn btn-secondary' data-bs-dismiss='modal'>Close</button>
                       <button type='submit' name='insertdata' class='btn btn-primary'>Add Car</button>


</div> ";

** php 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)) {

        $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'];

        $query = "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');";
        $query_run = mysqli_query($connection, $query);
        if ($query_run) {
            echo "<h2>Added successfully</h2>";
            echo "<a href='index.php'>home</a>";
        } else {
            echo 'Error! ' . mysqli_error($connection);
        mysqli_close($db_connection); // close the database connection

    } else {
        echo "<h3 class='errorH'>Error! <br>The following error(s) occurred:</h3>";
        foreach ($errors as $msg) {
            echo "<div class='error'>$msg </div>";

Read more here: https://stackoverflow.com/questions/67010472/i-want-to-make-my-radio-buttons-sticky-in-my-form-in-php

Content Attribution

This content was originally published by Java Lang at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: