【问题标题】:how to prevent html checkboxes, radiogroups and textarea reset if submit is unsuccessful如果提交不成功,如何防止 html 复选框、radiogroups 和 textarea 重置
【发布时间】:2012-05-12 03:43:13
【问题描述】:

我正在使用表单在我的网站上注册用户,并且我有验证码安全性。一切都运行良好,但我面临的唯一问题是,如果我输入了错误的验证码或以某种方式刷新页面,用户输入的所有数据都会被清除。

我现在知道如何防止文本字段被重置,但我仍然需要知道如何为复选框、radiogroups 和 textarea 做同样的事情。

我希望实现的是,即使输入的验证码错误并且提交了表单,表单也应该具有复选框、radiogroups 和 textarea 作为用户填写的内容,不包括验证码字段,以节省用户在重新填写表格。

如何做到这一点?我的表单是html,处理页面是php

【问题讨论】:

  • 将选择存储在 php 中并将它们传输到输出,以便表单填充已输入的值。有 html 标签,如 value=..." 或 checked="checked" 用于单选框。
  • 你能举个例子吗?
  • 您正在使用 $_POST 数组或 php 中的输入过滤器函数获取数据,对吧?好吧,如果验证码错误,请更新将显示的表单以包含用户输入的数据,并且可能 html 针对 xss 对其进行转义。
  • 是的,我正在使用 $_POST 获取数据,但是如何将它们应用于复选框并选择菜单
  • 您的表单是如何生成的?它是一个普通的 htm 文件,您可以在其中插入 php 内容吗?您可以包含它以传递内容或将表单提交的内容存储在会话数据中以供进一步使用。

标签: php html checkbox radio-button reset


【解决方案1】:

这样的事情应该可以工作。

<input type="checkbox" name="agree" value="yes" <?php echo ($_POST['agree']=='yes' ? 'checked="checked"' : '');?> />

【讨论】:

  • textarea 和选择菜单怎么样?
【解决方案2】:

一些输入的示例,脚本顶部的监视器应该为其他输入提供线索:

<?php
/*monitor the POST request, delete in production*/
echo "<pre>";
print_r($_POST);
echo "</pre>";

if($_SERVER['REQUEST_METHOD'] === 'POST')
{
    //check things like captcha / empty fields first, 
    //if something fails: fill variables
    $postedText     = isset($_POST['textInput']) ? $_POST['textInput'] : null;
    $postedCheckbox = isset($_POST['checkboxInput']) ? 1 : 0;
    $postedRadio    = isset($_POST['radioInput']) ? $_POST['radioInput'] : null;
    $postedSelect   = $_POST['selectInput'];
    $postedTextarea = isset($_POST['textareaInput']) ? $_POST['textareaInput'] : null;
}
?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <form method="post">
            <label for="textInput">Name:</label>
            <input type="text" 
                   id="textInput" 
                   name="textInput" 
                   value="<?php echo (isset($postedText) ? $postedText : 'text') ;?>"
                   />
            <br />
            <label for="checkboxInput">Checkbox:</label>
            <input type="checkbox" 
                   id="checkboxInput" 
                   name="checkboxInput" 
                   <?php echo (isset($postedCheckbox) && $postedCheckbox === 1 ? "checked='checked'" : '') ;?>
                   />
            <br />
            <label for="rbInput">Radio:</label>
            <input type="radio" 
                   id="rbInputMale" 
                   name="rbInput" 
                   value="male"
                   <?php echo (isset($postedRadio) && $postedRadio === 'male' ? "checked='checked'" : '') ;?>
                   /> male
            <input type="radio" 
                   id="rbInputFemale" 
                   name="rbInput" 
                   value="female"
                   <?php echo (isset($postedRadio) && $postedRadio === 'female' ? "checked='checked'" : '') ;?>
                   /> female
            <br />
            <label for="selectInput">Select</label>
            <select name="selectInput"
                    id="selectInput">
                <?php
                $options    = array(
                    'one'   => 'one',
                    'two'   => 'two',
                    'three' => 'three'
                );
                foreach ($options as $key => $value)
                {
                    ?>
                <option value="<?php echo $value;?>"
                        <?php 
                        if(isset($postedSelect))
                        {
                            echo ($value === $postedSelect ? "selected='selected'" : '');
                        }
                            ?>
                        >
                            <?php echo $key;?>
                </option>
                <?php
                }
                ?>
            </select>
            <br />
            <label for="textareaInput">textarea</label>
            <textarea name="textareaInput" id="textareaInput"><?php echo (isset($postedTextarea) ? $postedTextarea : '');?></textarea>
            <br />
            <input type="submit" value="submit" />
        </form>
    </body>
</html>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-30
    • 1970-01-01
    • 2014-03-05
    • 2022-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多