PHP - Form Elements Resetting

2019-08-16 08:56发布

问题:

I have made a simple form with a checkbox that echos out true or false depending on if the checkbox is set or not, however every time i click submit once the checkbox control has been ticked it seems to reset.

How can i modify my code to store the value of any form controls so if i tick the box it remains ticked after i have clicked submit?

<?php

// Setup Variables
$Result = '';

if (isset($_POST["submit"])) {

    //Use Captials?
    if(isset($_POST['formCheckbox']) && $_POST['formCheckbox'] == 'Yes') {
        $Result = "true";
    } else {
        $Result = "false";
    }

}

?>

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Bootstrap Contact Form</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
  </head>
  <body>
    <div class="container">
        <div class="row">
            <div class="col-md-6 col-md-offset-3">
                <h1 class="page-header text-center">Contact Form Example</h1>

                <form class="form-horizontal" role="form" method="post" action="index.php">

                    Checkbox
                    <input type="checkbox" name="formCheckbox" value="Yes"/>

                    <div class="form-group">
                        <div class="col-sm-10 col-sm-offset-2">
                            <input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
                        </div>
                    </div>

                </form> 

                <div class="col-sm-10 col-sm-offset-2">
                    <?php echo $Result; ?>  
                </div>

            </div>
        </div>
    </div>   
    <script src="js/jquery.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
  </body>
</html>

回答1:

Add the checked property & Change the way the check box is displayed:

<?php

// Setup Variables
$Result = '';

if (isset($_POST["submit"])) {

    //Use Captials?
    if(isset($_POST['formCheckbox']) && $_POST['formCheckbox'] == 'Yes') {
        $Result = "true";
    } else {
        $Result = "false";
    }

}

?>

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Bootstrap Contact Form</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
  </head>
  <body>
    <div class="container">
        <div class="row">
            <div class="col-md-6 col-md-offset-3">
                <h1 class="page-header text-center">Contact Form Example</h1>

                <form class="form-horizontal" role="form" method="post" action="index.php">

                    Checkbox
                    <?php 
                    if(isset($_POST['formCheckbox']) && $_POST['formCheckbox'] == 'Yes') {
                        echo '<input type="checkbox" name="formCheckbox" value="Yes" checked/>';
                    }
                    else
                        echo '<input type="checkbox" name="formCheckbox" value="Yes"/>';
                    ?>

                    <div class="form-group">
                        <div class="col-sm-10 col-sm-offset-2">
                            <input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
                        </div>
                    </div>

                </form> 

                <div class="col-sm-10 col-sm-offset-2">
                    <?php echo $Result; ?>  
                </div>

            </div>
        </div>
    </div>   
    <script src="js/jquery.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
  </body>
</html>