Reputation: 115
I have created a login form with CodeIgniter. To test the form, I submit incorrect data, I get the correct information back and the form is redisplayed. If I correct the errors and resubmit the uri segment is appended to the URL.
So I call the app with localhost/myapp
, the login form is displayed. On submission the url change to localhost/myapp/controller/authenticate
. When submitting again the URL change to localhost/myapp/controller/authenticate/controller/authenticate
What is the problem here?
View
<form action="<?php echo base_url();?>/welcome/authenticate" method="post" id="loginfrm">
<input type="text" name="username" /><?php echo form_error('username', '<div class="error">', '</div>'); ?><br />
<input type="password" name="password" /><?php echo form_error('password', '<div class="error">', '</div>'); ?><br />
<input type="submit" value="Login" />
</form>
controller
public function index()
{
$this->load->view('welcome_message');
}
public function authenticate()
{
$this->form_validation->set_rules('username', 'Username', 'trim|required');
$this->form_validation->set_rules('password', 'Password', 'trim|required');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('welcome_message');
}
else
{
echo $this->input->post('username') . " -->> " . $this->input->post('password');
}
}
}
Upvotes: 1
Views: 1096
Reputation: 4250
Always use redirect in function where forms are processed this prevents the form re-submission in your case if everything works fine and your view is loaded when user tries to refresh the page he will be asked to resubmit the form. Redirect function changes the url in browser address bar so user will no longer be asked for form re-submission.
Upvotes: 1