如何通过在html中提交表单来防止表单被清空? [英] How to prevent forms from emptying inputs by submiting the form in html?

查看:1624
本文介绍了如何通过在html中提交表单来防止表单被清空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个使用php和html的表单。
此表单具有验证控制。
当我提交表单时,所有的文本框都变空了,如果用户填写了例如5个输入中的3个,因为如果他/她现在必须再次填充那3个输入。
如何通过提交表单来阻止表单清空输入?

I'm writing a form in using php and html. This form has validation control. when I submit the form all the textboxes get empty and if the user has filled for example 3 of 5 inputs , because if those two inputs he/she now has to fill those 3 inputs again. How can I prevent the form from emptying the inputs by submitting the form?

推荐答案

如果您的验证脚本和表单代码位于同一个文件中,您可以将$ _POST值放入表单中。

If you validation script and form code are in this same file you can just put $_POST values into form.

<?php
$field1 = '';
$field2 = '';
...
$field5 = '';
if($_SERVER['REQUEST_METHOD']=="POST"){
    $field1 = $_POST['field1'];
    $field2 = $_POST['field2'];
    ...
    $field5 = $_POST['field5'];

    if(validation is true){
     //do something
    }
}
    ?>

    <form method="post">
       <input type="text" name="field1" value="<?= $field1 ?>" >
       <input type="text" name="field2" value="<?= $field2 ?>" >
        ...
       <input type="text" name="field5" value="<?= $field5 ?>" >
       <input type="submit" value="submit" >
    </form>

在评论RiggsFolly后编辑。

EDITED after comment RiggsFolly.

这篇关于如何通过在html中提交表单来防止表单被清空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆