0

I'm currently developing a system in CodeIgniter that requires user authentication via login & registration as a base in order to use the rest of the system. I've been having a 404 Not Found issue on my system whenever I try to call a Controller function, this is particularly prevalent on form submissions. I've posted my related code from my Controller, Model, View, Routes and Htaccess below. I'm not entirely sure whats causing the 404 not found error, any help would be greatly appreciated.

Controller:

// handles register page
public function register() {
    $this->load->helper('form');
    $this->load->library('form_validation');

    $data['title'] = "Register";

    $this->form_validation->set_rules('fullname','Full Name','required');
    $this->form_validation->set_rules('accountid','Account ID','required');
    $this->form_validation->set_rules('password','Password','required');
    $this->form_validation->set_rules('type','Account Type','required');

    if ($this->form_validation->run() === FALSE) {
        $this->load->view('register', $data);
    } else {
        $fullname = $this->input->post('fullname');
        $username = $this->input->post('username');
        $password = md5($this->input->post('password'));
        $accounttype = $this->input->post('type');

        $this->system->registeruser($username, $password, $fullname, $accounttype);
        echo "User Registration Complete";
    }
}

Model:

public function registeruser($accountid, $password, $fullname, $accounttype) {
    $query = "INSERT INTO users VALUES($fullname','$accountid','$password','$accounttype')";
    $this->db->query($query);
}

View:

<?php
    echo form_open('main/register');
    echo validation_errors(); ?>
        <div id="name-input">
            <?php
                $data = array(
                    'name' => 'fullname',
                    'value' => $this->input->post('fullname'),
                    'placeholder' => 'Full Name',
                    'class' => '',
                    'style' => 'width:100%; padding:0.5em;' );

                echo form_input($data);
            echo "</p>"; ?>
        </div>

        <div id="username-input">
            <?php
                $data = array(
                    'name' => 'accountid',
                    'value' => $this->input->post('accountid'),
                    'placeholder' => 'User ID',
                    'class' => 'username',
                    'style' => 'width:100%; padding:0.5em;' );

                echo form_input($data);
            echo "</p>"; ?>
        </div>

        <div id="password-input">
            <?php
                $data = array(
                    'name' => 'password',
                    'value' => $this->input->post('password'),
                    'placeholder' => 'Password',
                    'class' => 'passwordd',
                    'style' => 'width:100%; padding:0.5em;' );

                echo form_password($data);
            echo "</p>"; ?>
        </div>

        <div id="account-type">
            <?php
                $options = array(
                    'student' => 'Student',
                    'lecturer' => 'Lecturer' );

                $data = array(
                    'name' => 'type',
                    'style' => 'width:100%; padding:0.5em;' );

                echo form_dropdown($data, $options);
            echo "</p>"; ?>
        </div>

        <div id="submit-button">
            <?php
                $data = array(
                    'name' => 'register',
                    'value' => 'Register Account',
                    'class' => 'register',
                    'style' => 'width:100%; padding:0.5em;' );

                echo form_submit($data); ?>
        </div>
<?php echo form_close(); ?>

Routes:

$route['main/register'] = 'main/register';
$route['default_controller'] = 'main';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

Htaccess:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

All the code above relates to my registration page / it's related functions, I have multiple of these errors on my system currently but they all seem to be the same thing, I figured solving one may solve the rest as well.

2 Answers 2

1

I don't know the exact cause of your problem, but following solution should work.

Remove

echo form_open('main/register');

line and add

echo "<form action='' method='post' >";

(leave the action attribute empty, so it will submit data to the current page)

Remove

$route['main/register'] = 'main/register';

line.

Extra suggestion: Don't use md5(), because it is not secure. Use password_hash() instead.

Sign up to request clarification or add additional context in comments.

Comments

0

Try removing the route $route['main/register'] = 'main/register';. Also, give method POST to the form. And input field names do not match in controller as well as model.


Edit:

Form action should be a complete URL-
echo form_open(base_url()."/controllername/mehod"); In your case it should be echo form_open(base_url()."/main/register");

If you haven't set base_url then

Step 1: Open the config.php file under application\config folder

step 2: Set the value of $config['base_url'] to your website path

$config['base_url'] = "http://localhost/foldername/index.php";

That should help. :)

3 Comments

So I removed the route from routes.php, fixed the input field names but I'm still getting the error come up, how would I add method="post" to the echo form_open() ?
$attribute['method'] = 'POST'; echo form_open(THE_ACTION_URL, $attribute);
I tried adding that, it still doesn't work, still getting the 404 not found.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.