1

I'm calling these functions from a controller to get the form and the values from the form. My question is, how can I keep the values in the form after a submit fails? I've tried something like this:

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

but can't get it to work.

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;
}
4

3 回答 3

1

[edit: run this on localhost]

autocomplete不适用于所有浏览器;而且,这只是一个提示,所以cookies是唯一的选择。下面的解决方案可以正常工作,但是如果其他用户为这个流行的请求提供替代方式,那就太好了: 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>

图像-1 图像-2 图像-3 图像 4

于 2013-01-24T17:30:38.453 回答
0

在您的表单之前,实例化将接收$_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; ?>" />
于 2013-01-24T17:44:37.713 回答
0
<html>
  <body>

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

  </body>
</html>
于 2013-01-24T16:23:39.047 回答