Ayush
Ayush

Reputation: 42450

Controller repeating in URL in CodeIgniter

I have a Controller called Register that looks like this:

class Register extends CI_Controller
{
    public function index()
    {
        $this->load->view('register');
    }

    public function create()
    {
        $this->load->library('form_validation');
        //set rules here

        if ($this->form_validation->run() == TRUE)
        {
            //everything checks out
        }
        else
        {
            $this->load->view('register');
        }
    }
}

The view contains a form that posts to register/create.

My problem is, when I go to localhost/register and fill out the form incorrectly, the form reloads, but the url is now:

localhost/register/register/create

If I enter it incorrectly again

localhost/register/register/register/create

and so on...

I'm guess this is an htaccess and I've tried using some standard CodeIgniter htaccess files available online, but none fix this issue. I'm using this on a WAMP server.

EDIT

The view file only consists of the form, for now. I prefer using direct HTML instead of the CodeIgniter Form helper.

<form id="register_form" method="post" action="register/create" title="Create an Account">
    <!-- input fields -->
</form>

Upvotes: 1

Views: 3031

Answers (4)

Khamad Ali
Khamad Ali

Reputation: 31

Add base_url()in your url :

<a href="<?=base_url()?>/mypage">

Upvotes: 0

Robin Castlin
Robin Castlin

Reputation: 10996

Use full url. Either current_url() or site_url('register') in your form's action.

It's good practice you use either of these or preffix with base_url() since that allows your site to run without having to be the domain path.
For instance you can, if needed run the site on example.com/my_site/ instead of only being able to run on example.com.

For fullest possible control of your form's action, always start on http:// and avoid relative urls (action="register" for instance).

Upvotes: 4

Luis Lora
Luis Lora

Reputation: 83

I had the same problem and I could address it by putting in my base_url the "http://" before my domain like this:

$config['base_url'] = 'http://example.com';

Upvotes: 2

birderic
birderic

Reputation: 3765

Change the action parameter of your form from register/create to /register/create. You're posting to a relative URL. By adding the slash to the beginning of the action parameter you're making the URL absolute. Therefore the form will always post to http://domain-name/register/create.

Upvotes: 3

Related Questions