stergosz
stergosz

Reputation: 5860

codeigniter form submit removes get variables from url

I have a url which has some get variables in it like:

domain.com/[email protected]&token=abc123

inside the recover controller i load a form with POST method

so when i submit the form i load again the recover controller but my GET variables disappear and return to

domain.com/recover

so how can i make sure the GET variables retain in the URL after submitting the form?

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(); ?>

Controller:

$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);

Upvotes: 0

Views: 2047

Answers (2)

safarov
safarov

Reputation: 7804

Change this line: <?php echo form_open("recover"); ?>

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

it will create all get values as hidden input. When you submit form, they will send with method that you defined for form.

Or you can just write manually:

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

Upvotes: 2

Beeblbrox
Beeblbrox

Reputation: 351

Manually render the form tag in php or the html and add some javascript

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

Much better js is available for writing this...

Upvotes: 0

Related Questions