CodeIgniter Forums
New issues with form validation - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: New issues with form validation (/thread-64949.html)



New issues with form validation - koficypher - 04-13-2016

hi, am here again. I did some house cleaning on my previous code and am still having issues with my form validation(dunnoe if that's really the cause). Am creating a bus reservation system as a school project and am av created a registration form for a user with a controller called user.php with function called register_user(). I also have a model named model_user.php with a function insert_user() . My view file is view_register.php and if a user successfully fills and submits the form a view_register_success is supposed to appear telling the user that the have successfully registered for an account. Now that doesn't happen instead a 404 page appears and this is how this url in the browser appears  http://localhost/busticket/user/user/register_user, i have no idea on what to do. Please help!!

Code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class User extends CI_Controller {

            public function __construct()
       {
               parent::__construct();
               $this->load->model('model_user');
       
       }

       public function index()
       {
           $this->register_user();
       }

    public function register_user()
    {
        $this->load->library('form_validation');
        //$this->form_validation->set_error_delimiters('<div class="alert alert-danger" role="alert"><span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span><span class="sr-only">Error:</span>', '</div>');
         $data['title'] = 'Register';



        $this->form_validation->set_rules('firstname', 'First Name', 'trim|required|min_length[3]|max_length[15]');
        $this->form_validation->set_rules('lastname', 'Last Name', 'trim|required|min_length[3]|max_length[15]');
        $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[6]|max_length[15]|is_unique[user.username]');
        $this->form_validation->set_rules('email', 'Email', 'trim|required|min_length[6]|max_length[30]|valid_email|is_unique[user.email]');
        $this->form_validation->set_rules('password', 'Password', 'required|min_length[6]|max_length[20]|matches[password_conf]');
        $this->form_validation->set_rules('password_conf', 'Confirm Password', 'required|min_length[6]|max_length[20]');

        if ( $this->form_validation->run() === FALSE) {

            $this->load->view('templates/header', $data);
            // $this->load->view('templates/navigation');
            $this->load->view('frontend/view_register');
            $this->load->view('templates/footer');

        }
        else
        {
            
             $this->model_user->insert_user();
             //echo 'good';

            //$this->load->view('templates/header');
            //$this->load->view('templates/navigation');
            $this->load->view('frontend/view_register_success');
            //$this->load->view('templates/footer');

        }


        
    }
}

    
---------------------------------------------------
<div class="container">
<div class="row">
    <div class="col-md-6 col-md-offset-3">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h4>User Registration Form</h4>
            </div>
            <div class="panel-body">
               <form action="user/register_user" method="post">
                <div class="form-group">
                    <label for="name">First Name</label>
                    <input class="form-control" name="firstname" placeholder="Your First Name" type="text" value="<?php echo set_value('firstname'); ?>"/>
                    <span class="text-danger"><?php echo form_error('firstname'); ?></span>
                </div>

                <div class="form-group">
                    <label for="name">Last Name</label>
                    <input class="form-control" name="lastname" placeholder="Last Name" type="text" value="<?php echo set_value('lastname'); ?>" />
                    <span class="text-danger"><?php echo form_error('lastname'); ?></span>
                </div>

                <div class="form-group">
                    <label for="name">Username</label>
                    <input class="form-control" name="lastname" placeholder="Username" type="text" value="<?php echo set_value('username'); ?>" />
                    <span class="text-danger"><?php echo form_error('username'); ?></span>
                </div>
                
                <div class="form-group">
                    <label for="email">Email ID</label>
                    <input class="form-control" name="email" placeholder="Email-ID" type="text" value="<?php echo set_value('email'); ?>" />
                    <span class="text-danger"><?php echo form_error('email'); ?></span>
                </div>

                <div class="form-group">
                    <label for="subject">Password</label>
                    <input class="form-control" name="password" placeholder="Password" type="password" />
                    <span class="text-danger"><?php echo form_error('password'); ?></span>
                </div>

                <div class="form-group">
                    <label for="subject">Confirm Password</label>
                    <input class="form-control" name="password_conf" placeholder="Confirm Password" type="password" />
                    <span class="text-danger"><?php echo form_error('password_conf'); ?></span>
                </div>

                <div class="form-group">
                    <button name="submit" type="submit" class="btn btn-default">Signup</button>
                    <button name="cancel" type="reset" class="btn btn-default">Cancel</button>
                </div>
            </form>    
                
            
    
-------------------------------------------------------
| controller and method URI segments.
|
| Examples:    my-controller/index    -> my_controller/index
|        my-controller/my-method    -> my_controller/my_method
*/
$route['default_controller'] = 'home';
$route['user/register'] = 'user/register';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
---------------------------------------------------------
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Model_user extends CI_Model {
       public function __construct()
       {
             parent::__construct();
             $this->load->database();
             
       }

       public function insert_user()
       {
       
           

            $data = array(
                          'firstname' => $this->input->post('firstname'),
                          'lastname' => $this->input->post('lastname'),
                          'username' => $this->input->post('username'),
                          'email' =>    $this->input->post('email'),
                          'password' => $this->input->post('password'),
                         
                         );

          $this->db->insert('user', $data);

          }
}



RE: New issues with form validation - arma7x - 04-13-2016

<form action="<?php echo site_url('user/register_user');?>" method="post">


RE: New issues with form validation - Wouter60 - 04-13-2016

In your controller:
PHP Code:
$this->load->helper('form'); 

In your registration view:
PHP Code:
echo form_open('user/register_user');
//.... lines of code that build your fields; the form helper makes life easy!
echo form_close(); 

Remove this line from your routes.php file:
PHP Code:
$route['user/register'] = 'user/register'
A route is only useful if you want to redirect the given url to another controller/method combination.


RE: New issues with form validation - koficypher - 04-13-2016

it still doesn't work!! The form resets with all values intact except for password and confirm password because I didn't do set_value() on them in the view file. Something very odd happens tho, the set value for username resets and becomes that for last name leaving the username blank and displays an error saying the username is required meanwhile I had previously filled the username before hitting the submit button. The url then becomes http://localhost/busticket/index.php/user/register_user. Really need help!!!


RE: New issues with form validation - koficypher - 04-13-2016

(04-13-2016, 01:13 PM)Wouter60 Wrote: In your controller:
PHP Code:
$this->load->helper('form'); 

In your registration view:
PHP Code:
echo form_open('user/register_user');
//.... lines of code that build your fields; the form helper makes life easy!
echo form_close(); 

Remove this line from your routes.php file:
PHP Code:
$route['user/register'] = 'user/register'
A route is only useful if you want to redirect the given url to another controller/method combination.
tried that previously, am auto loading the form helper alongside the url helper too. but i'll try again and see if it gives a different output


RE: New issues with form validation - arma7x - 04-13-2016

(04-13-2016, 01:25 PM)koficypher Wrote: it still doesn't work!! The form resets with all values intact except for password and confirm password because I didn't do set_value() on them in the view file. Something very odd happens tho, the set value for username resets and becomes that for last name leaving the username blank and displays an error saying the username is required meanwhile I had previously filled the username before hitting the submit button. The url then becomes http://localhost/busticket/index.php/user/register_user. Really need help!!!

Does 404 problem solve now? You sent request from 'index' to 'user_register'. Remove this from user controller,
PHP Code:
public function index()
 {
 
    $this->register_user();
 } 

And add this line in routes.php
$route['user'] = 'user/register_user';

Or without edit routes.php, replace $this->register_user(); with redirect('user/register_user') in User controller.


RE: New issues with form validation - koficypher - 04-13-2016

(04-13-2016, 01:49 PM)arma7x Wrote:
(04-13-2016, 01:25 PM)koficypher Wrote: it still doesn't work!! The form resets with all values intact except for password and confirm password because I didn't do set_value() on them in the view file. Something very odd happens tho, the set value for username resets and becomes that for last name leaving the username blank and displays an error saying the username is required meanwhile I had previously filled the username before hitting the submit button. The url then becomes http://localhost/busticket/index.php/user/register_user. Really need help!!!

Does 404 problem solve now? You sent request from 'index' to 'user_register'. Remove this from user controller,
PHP Code:
public function index()
 {
 
    $this->register_user();
 } 

And add this line in routes.php
$route['user'] = 'user/register_user';

Or without edit routes.php, replace  $this->register_user(); with redirect('user/register_user') in User controller.

hate to be a bother tho but none of the above solutions work. Sad


RE: New issues with form validation - Wouter60 - 04-13-2016

Hi, the mix-up of field entries in your form is caused by one or more mistakes in the view file.
PHP Code:
<label for="name">Username</label>
<
input class="form-control" name="lastname" placeholder="Username" type="text" value="<?php echo set_value('username'); ?>" /> 

You intend to ask for the Username, but your field is named "lastname".

The problem that index.php now shows up in your URL's, may be caused by settings in your config/config.php file.
Please check the values for:
$config['base_url']
$config['index_page']

The first one should hold the true base url of your website, without index.php.
The last one should be empty.

Also check your .htaccess file to make sure the index.php is hidden from the url.

Hope this will help you.


RE: New issues with form validation - koficypher - 04-14-2016

(04-13-2016, 10:50 PM)Wouter60 Wrote: Hi, the mix-up of field entries in your form is caused by one or more mistakes in the view file.
PHP Code:
<label for="name">Username</label>
<
input class="form-control" name="lastname" placeholder="Username" type="text" value="<?php echo set_value('username'); ?>" /> 

You intend to ask for the Username, but your field is named "lastname".

The problem that index.php now shows up in your URL's, may be caused by settings in your config/config.php file.
Please check the values for:
$config['base_url']
$config['index_page']


thanks a lot!!! its works now.

The first one should hold the true base url of your website, without index.php.
The last one should be empty.

Also check your .htaccess file to make sure the index.php is hidden from the url.

Hope this will help you.