PHP - 表单元素重置 [英] PHP - Form Elements Resetting

查看:17
本文介绍了PHP - 表单元素重置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个带有复选框的简单表单,根据是否设置了复选框来显示真或假,但是每次我在复选框控件被勾选后单击提交时,它似乎都会重置.

如何修改我的代码以存储任何表单控件的值,以便如果我勾选该框,它在我单击提交后仍保持勾选状态?

<!DOCTYPE html><html lang="zh-cn"><头><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 联系表</title><link rel="stylesheet" href="css/bootstrap.min.css"><身体><div class="容器"><div class="row"><div class="col-md-6 col-md-offset-3"><h1 class="page-header text-center">联系表单示例</h1><form class="form-horizo​​ntal" role="form" method="post" action="index.php">复选框<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 class="col-sm-10 col-sm-offset-2"><?php echo $Result;?>

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

解决方案

添加选中的属性 &更改复选框的显示方式:

<!DOCTYPE html><html lang="zh-cn"><头><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 联系表</title><link rel="stylesheet" href="css/bootstrap.min.css"><身体><div class="容器"><div class="row"><div class="col-md-6 col-md-offset-3"><h1 class="page-header text-center">联系表单示例</h1><form class="form-horizo​​ntal" role="form" method="post" action="index.php">复选框<?phpif(isset($_POST['formCheckbox']) && $_POST['formCheckbox'] == 'Yes') {echo '<input type="checkbox" name="formCheckbox" value="Yes" 选中/>';}别的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 class="col-sm-10 col-sm-offset-2"><?php echo $Result;?>

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

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>

解决方案

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>

这篇关于PHP - 表单元素重置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
PHP最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆