保留值的形式提交后PHP(Keep value in form after submitting P

2019-08-08 03:30发布

我从一个控制器调用这些函数来获取表格,并从表单中的值。 我的问题是,我怎么能保持价值的表格后,一个提交失败? 我已经试过这样的事情:

 <input type="text" name="myField1" value="<?php echo isset($_POST['myField1']) ? $_POST['myField1'] : '' ?>" />

但不能让它开始工作。

private $m_username = 'username';
private $m_password = 'password';
private $m_registerButton = 'registerButton';

public function RegisterUserBox(){
    return
    '<form method="post">
            <fieldset>
                Username: <input type="text" name="'.$this->m_username.'" />
                Password: <input type="password" name="'.$this->m_password.'" />
                <input type="submit" value="Register" name="'.$this->m_registerButton.'"/>
            </fieldset>
     </form>';

}

public function GetUsername(){
    if(isset($_POST[$this->m_username])){
        return $_POST[$this->m_username];
    }
}

public function GetPassword(){
    if (isset($_POST[$this->m_password])){
        return $_POST[$this->m_password];
    }
}

public function TriedToRegister(){
    if (isset($_POST[$this->m_registerButton])){
        return true;
    }
    return false;
}

Answer 1:

[edit: run this on localhost]

autocomplete不能在所有的浏览器; 此外,它只是一个提示,所以饼干是唯一的选择。 下面的解决方案工作正常,但是它如果其他用户贡献替代方式对这种流行的要求是巨大的 Keep value in form after submitting

<html>
  <head>
    <script>
        function setCookie(c_name, value, exdays) {
            var exdate = new Date();
            exdate.setDate(exdate.getDate() + exdays);
            var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
            document.cookie = c_name + "=" + c_value;
        }

        function getCookie(c_name) {
            var i, x, y, ARRcookies = document.cookie.split(";");
            for (i = 0; i < ARRcookies.length; i++) {
                x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
                y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
                x = x.replace(/^\s+|\s+$/g, "");
                if (x == c_name) {
                    return unescape(y);
                }
            }
        }

        function store() {
            var inputs, index;

            inputs = document.getElementsByTagName('input');
            for (index = 0; index < inputs.length - 1; ++index) {
                setCookie(inputs[index].name,inputs[index].value,1);
            }
            return false;
        }
    </script>
  </head>

  <body>
    <form method="post" action="back.php" onsubmit="store()" >
      firstname<input type="text" name="firstname">
      lastname<input type="text" name="lastname">
      emailid<input type="text" name="emailid">
      <input type="submit" >
    </form>
    <script>
        (function load(){
            var inputs, index;

            inputs = document.getElementsByTagName('input');
            for (index = 0; index < inputs.length - 1; ++index) {
                inputs[index].value = getCookie(inputs[index].name);
            }
            return false;
        })();
    </script>
  </body>
</html>



Answer 2:

<html>
  <body>

    <form method="post" action="back.php" autocomplete="on" >
      <input type="text" autocomplete="on" >
      <input type="submit" >
    </form>

  </body>
</html>


Answer 3:

你的表格前,实例将接收对象$_POST数据

$userbox = new Userbox;

然后处理$_POST数据,如果有的话:

if(isset($_POST['submit']) && $_POST['submit'] === 'userboxsubmit'){
  $userbox->process_post(); 
}

然后输出的形式:

<input type="text" name="myField1" value="<?php echo $userbox->myField1; ?>" />


文章来源: Keep value in form after submitting PHP
标签: php forms