I am trying to return the values of checkbox if the checkbox is checked before form submission it should remain checked after the post method. The checkbox works fine and gets updated in database i only need to retain checkbox values.....Thanks for help in advance

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
    <title>Login Form</title>
</head>
<?php 
$firstname = trim($_POST['firstname']);
$lastname = trim($_POST['lastname']);
$email = trim($_POST['email']);
$zip = trim($_POST['zip']);
$vehicle = $_POST['vehicle'];
$gender = $_POST['gender'];
$male_status = 'unchecked';
$female_status = 'unchecked';


    if(isset($_POST['submitform']))
    {

       $errorstr = "";

    if($firstname == "")
    {
       $errorstr .= "<li>Please enter the firstname</li>";
    }   
    if($lastname == "")
    {
       $errorstr .= "<li>Please enter the lastname</li>";
    }   

    if($email == "")
    {
       $errorstr .= "<li>Please enter the email address</li>";
    } else if (!preg_match('/^[^\W][a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*\@[a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*\.[a-zA-Z]{2,4}$/',$email)) 
            {
               $errorstr .= "<li>Please enter the valid email address</li>";
            }

    if(isset($_POST['zip']))
    {
    if($zip == "")
    {
    }else if (!preg_match("/^([0-9]{5})(-[0-9]{4})?$/i",$zip))
                    {
                        $errorstr .=  "<li>Please enter the valid zipcode id</li>"; 
                    }
    }       

    if($vehicle == "")
    {
      $errorstr .= "<li>Please select atleast one car</li>";
    } else
           {
                $N = count($vehicle);

                for($i=0; $i < $N; $i++)
                {

                }
           }

            $selected_vehicle = "";
            foreach ($vehicle as $vehicle) 
            {
                $selected_vehicle .= $vehicle . ", " ;

            }
                  $selected_vehicle = substr($selected_vehicle, 0, -2);



    if($gender == "male")
    {
            $male_status = 'checked';
    } else if ($gender == "female")
            {
                  $female_status = 'checked';
            } else
                  {
                      $errorstr .=  "<li>Please select atleast one gender</li> ";
                  } 

    if(!empty($errorstr))
    {
        echo "<Strong>Please correct the error on this page</Strong>";
        echo "<ul>. $errorstr .</ul>";

    }


    if(empty($errorstr))
        {
            $db_handle = mysql_connect("localhost","root","");
        if (!$db_handle)
        {
               die('Could not connect: ' . mysql_error());
        } 

    $database="test";  

    $db_found = mysql_select_db($database,$db_handle);

       if($db_found)
       {
          $SQL = "INSERT INTO register(firstname,lastname,email,zip,vehicle,gender) VALUES ('$firstname','$lastname','$email','$zip','" . $selected_vehicle. "','$gender')";
          $result= mysql_query($SQL);
          mysql_close($db_handle);
          echo "<br/>Record added to database";
       }  
          else 
          {
             print "<br/>Database NOT Found ";
             mysql_close($db_handle);
          }
        }       


    }

?>

<body>
<form name="loginform" method="post" action="loginform.php">
<br>Firstname: <input type="text" name="firstname" value="<?php echo $firstname; ?>"/> <br/>
<br>Lastname:  <input type="text" name="lastname" value="<?php echo $lastname; ?>"/> <br/>
<br>Email:     <input type="text" name="email" value="<?php echo $email; ?>"/><br/>
<br>ZipCode:   <input type="text" name="zip" value="<?php echo $zip; ?>"/><br/>
<br>Cars:<br/> <input type="checkbox" name="vehicle[]" value="Toyota" <?php echo $vehicle; ?> />Toyota <br/>   
               <input type="checkbox" name="vehicle[]" value="Honda" <?php echo $vehicle; ?>  />Honda <br/>
               <input type="checkbox" name="vehicle[]" value="VW" <?php echo $vehicle; ?> />VW<br/>
               <input type="checkbox" name="vehicle[]" value="Lexus" <?php echo $vehicle; ?> />Lexus <br/>
               <input type="checkbox" name="vehicle[]" value="Acura" <?php echo $vehicle; ?>/>Acura<br/>
               <input type="checkbox" name="vehicle[]" value="Audi" <?php echo $vehicle; ?> />Audi<br/>
<br>Gender:<br>
<input type="radio" name="gender" value="male" <?PHP echo $male_status;?>/> Male<br />
<input type="radio" name="gender" value="female" <?PHP echo $female_status;?>/> Female <br />

<input type="submit" name="submitform" value="Submit">
<input type="reset" value="Reset">
</form>
</body>
</html>
Member Avatar for Zagga

Hi Prolaysarkar

If you name each of your checkboxes individually it makes it a bit easier to work with them individually.
For each of your checkbox input statements we can check if the box was ticked when the form was posted and add the HTML code to the input statement to show it is checked.

For example, this is what the first 2 checkboxes would look like

<?php
$che = "";
if (isset($_POST['Toyota'])){
    $che = " checked='checked'";
}
?>
<input type="checkbox" name="Toyota" value="Toyota"<?php echo $che; ?> />Toyota <br/>

<?php
$che = "";
if (isset($_POST['Honda'])){
    $che = " checked='checked'";
}
?>
<input type="checkbox" name="Honda" value="Honda"<?php echo $che; ?> />Honda <br/>

You would have to alter your $selected_vehicle variable to work on the new names.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.