php实现表单粘性例子

 

在填表单的时候,有时候会出现表单未填完就提交的情况,这时候若是想要回到原来的表单页面,一般之前填的内容都会消失掉。

故使用PHP实现回到原来表单但是填写数据不消失,代码🌰如下

 1<?php
    if (isset($_POST)) {
        $name = $_POST['username'] ?? NULL;
        $text = $_POST['text'] ?? NULL;
        if (isset($_POST['like'])) {
            foreach ($_POST['like'] as $key => $value)
                $check[$value] = "checked";
        }
    }
?>
<!DOCTYPE html>
<html>
<head>
    <title>FORM</title>
    <meta charset="utf-8">
    <link rel="stylesheet" type="text/css" href="http://cdn.amazeui.org/amazeui/2.7.2/css/amazeui.min.css">
</head>
<body>
    <div class="am-container">
    <form class="am-form" method="post" action="index.php">
        <legend>黏性表单</legend>
        <div class="am-form-group">
            <label for="doc-ipt-name">用户名</label>
            <input type="text" placeholder="给个名字吧" name="username" value=<?php if echo $name ?? NULL;?>>
        </div>
        <div class="am-form-group">
            <label for="doc-select-2">喜欢的编程语言</label>
            <label class="am-checkbox-inline">
                <input type="checkbox" value="php" name="like[]" <?php echo $check['php'] ?? NULL;?>> PHP
            </label>
            <label class="am-checkbox-inline">
                <input type="checkbox" value="python" name="like[]" <?php echo $check['python'] ?? NULL;?>> PYTHON
            </label>
            <label class="am-checkbox-inline">
                <input type="checkbox" value="java" name="like[]" <?php echo $check['java'] ?? NULL;?>> JAVA
            </label>
            <label class="am-checkbox-inline">
                <input type="checkbox" value="c++" name="like[]" <?php echo $check['c++'] ?? NULL;?>> C++
            </label>
        </div>
        <div class="am-form-group">
            <label for="doc-ta-1">有什么想说的</label>
            <textarea rows="5" id="doc-ta-1" name="text"><?php echo $text ?? NULL; ?></textarea>
        </div>
        <button type="submit" class="am-btn am-btn-default">提交</button>
    </form>
</div>
</body>
</html>

 

 

posted on 2016-07-22 01:03  Sshpark  阅读(654)  评论(0编辑  收藏  举报