PHP - 表单元素重置

时间:2015-04-08 21:42:02

标签: php forms twitter-bootstrap reset

我创建了一个带有复选框的简单表单,根据复选框是否设置,回显为真或假,但是每次勾选复选框控件后单击提交,它似乎都会重置。

如何修改我的代码以存储任何表单控件的值,如果我勾选方框,我点击提交后仍然会勾选?

<?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 个答案:

答案 0 :(得分:1)

添加已检查的属性&amp; 更改复选框的显示方式:

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