【问题标题】:Keep value in form after submitting PHP提交 PHP 后保持表单中的值
【发布时间】:2013-01-08 11:23:12
【问题描述】:

我从控制器调用这些函数来获取表单和表单中的值。我的问题是,提交失败后如何保留表单中的值?我尝试过这样的事情:

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

【问题讨论】:

  • 这对我来说很好,代码的顶部。它只是永远不会填写?
  • 我在使用最上面的代码时不断出现语法错误,无法弄清楚是什么问题。
  • 语法错误的文本是什么?
  • 我赞成你的回答,因为这是一个重要的概念

标签: php forms


【解决方案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>

【讨论】:

    【解决方案2】:
    <html>
      <body>
    
        <form method="post" action="back.php" autocomplete="on" >
          <input type="text" autocomplete="on" >
          <input type="submit" >
        </form>
    
      </body>
    </html>
    

    【讨论】:

    • 使用那个方法 :D 没有额外费用,简单的纯 html :D
    • 但这并没有保留字段中的值,只是预测值,对吧?
    • 它可以保留,但我正在所有浏览器上检查它,并且还在使用 javascript 方式来做到这一点
    【解决方案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; ?>" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-29
      • 2011-07-27
      • 1970-01-01
      • 1970-01-01
      • 2011-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多