codeigniter表单提交从url中删除get变量

时间:2012-03-25 10:24:20

标签: php url controller get

我有一个url,里面有一些get变量,如:

domain.com/recover?email=my@email.com&token=abc123

在恢复控制器内部我使用POST方法

加载表单

所以当我提交表单时,我再次加载恢复控制器,但我的GET变量消失并返回

domain.com/recover

那么如何在提交表单后确保GET变量保留在URL中?

HTML:

<?php echo form_open('recover'); ?>
    <label for="password1" class="fl" style="width:160px;">Your new Password</label>
    <input type="text" value="" id="password1" name="password1" style="width:308px;margin-top:0;margin-left:10px;" />
    <label for="password2" class="fl" style="width:160px;">Retype your new Password</label>
    <input type="text" value="" id="password2" name="password2" style="width:308px;margin-top:0;margin-left:10px;" />
    <input type="submit" value="Recover password" class="button_ui" name="submit" />
<?php echo form_close(); ?>

控制器:

$data = array(
    'errors' => '',
    'show_password_form' => false
);
$get_email = $this->input->get("email") ? trim($this->input->get("email")) : "";
$get_token = $this->input->get("token") ? trim($this->input->get("token")) : "";
if ($get_email != "" && $get_token != ""){
    $this->load->model("recover_model");
    $data["show_password_form"] = true;
    //check new passwords and update
    $submit = $this->input->post("submit_change") ? trim($this->input->post("submit_change")) : "";
    if ($submit){
        $password1 = $this->input->post("password1") ? trim($this->input->post("password1")) : "";
        $password2 = $this->input->post("password2") ? trim($this->input->post("password2")) : "";
        //if password1 is valid
        if ($this->recover_model->valid_password($password1)){
            //if password2 is valid
            if ($this->recover_model->valid_password($password2)){
            //if both are equal
                if ($password1 == $password2){
                    //update password
                }else{
                    $data["errors"] = 'Your passwords do not match.';
                }
            }else{
            $data["errors"] = 'Your password must be at least 6 characters.';
            }
        }else{
            $data["errors"] = 'Your password must be at least 6 characters.';
        }
    }
}
$this->load->view("account/recover/recover", $data);

2 个答案:

答案 0 :(得分:2)

更改此行:<?php echo form_open("recover"); ?>

要    <?php echo form_open('recover', $_GET); ?>

它会将所有获取值创建为隐藏输入。当您提交表单时,他们将使用您为表单定义的方法发送。

或者您可以手动编写:

<?php echo '<form method="post" accept-charset="utf-8" action="'.base_url().'recover?email='.$_GET['email].'&abc='.$_GET['token'].'" />'; ?>

答案 1 :(得分:0)

在php或html中手动渲染表单标记并添加一些javascript

<script type="text/javascript">
    document.write("<form method=\"POST\" action=\"");
    document.write(window.location.href);
    document.write("\">");
</script>

可以写出更好的js ...