0
votes

I am trying to load the RegisterController when i use a route with post request from an html form. When I click the submit button the RegisterController is not called.

When the register.php is accessed using register route with get request index method from RegisterController is loaded but when the same register route is accessed with POST request it expected to load register method. But it is not working.

Here is the routes.php file


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

//Login and Register for user and Admin

$route['register']['GET'] = 'Auth/RegisterController/index';
$route['register']['POST'] = 'Auth/RegisterController/register';


RegisterController.php file

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


class RegisterController extends CI_Controller
{
  
    public function __construct()
    {
        parent::__construct();
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->load->helper('url');
    }


    public function index()
    {
        // $this->load->view('template/header.php');
        $this->load->view('auth/register.php');
        // $this->load->view('template/footer.php');
    }


    public function register()
    {
        
        echo "i am inside register method of the register controller";
       $this->form_validation-> set_rules('first_name','First Name','trim|required|alpha');
       $this->form_validation-> set_rules('last_name','Last Name','trim|required|alpha');
       $this->form_validation-> set_rules('email','Email ID','trim|required|valid_email|is_unique[users.email]');
       $this->form_validation-> set_rules('password','Password','trim|required|mdS');
       $this->form_validation-> set_rules('cpassword','Confirm Password','trim|required|matches[password]');


       if($this->form_validation->run()==False)
       {
           //failed
           $this->index();

       }
       else
       {
           echo "I am here";
       }
    }
}


?>

Here is the Register.php file


<!DOCTYPE html>
<html lang="en">
<head>
    
    <title>Register Page</title>
</head>
<body>
<h4>Register Page</h4>







<form action="<?php  echo base_url('register') ?>" method="POST">



<label for="">First Name</label>
<input type="text" name="first_name">
<small><?php echo form_error('first_name'); ?></small>
<br>

<label for="">Last Name</label>
<input type="text" name="last_name">
<small><?php echo form_error('last_name'); ?></small>

<br>

<label for="">Email address</label>
<input type="email" name="email">
<small><?php echo form_error('email'); ?></small>

<br>

<label for="">Password</label>
<input type="password" name="password">
<small><?php echo form_error('password'); ?></small>

<br>

<label for="">Confirm Password</label>
<input type="password" name="cpassword">
<small><?php echo form_error('cpassword'); ?></small>

<br>

<button type="submit">Register Now</button>
</form>
</body>
</html>
1
You can validate it inside the controller, using if($_SERVER["REQUEST_METHOD"] == "POST") and you can use GETMurad Ali
Thank you for your response...But my problem is when i click register now button it is not loading the register method of the RegisterController so that i can continue with the form validation part.Ameen Shah
Well use $route['register'] = 'Auth/RegisterController/index'; and I hope the RegisterController must be inside the Auth folder.Murad Ali

1 Answers

0
votes

Let me write complete code modification for you.

Route.php

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

//Login and Register for user and Admin

$route['register'] = 'Auth/RegisterController/index';
$route['register'] = 'Auth/RegisterController/register';

Auth/RegisterController.php

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

class RegisterController extends CI_Controller
{
  
    public function __construct()
    {
        parent::__construct();
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->load->helper('url');
    }


    public function index()
    {
        if($_SERVER["REQUEST_METHOD"] == "GET"){
            // $this->load->view('template/header.php');
            return $this->load->view('auth/register.php');
            // $this->load->view('template/footer.php');
        }
        return 'Only GET Method Allowed';
    }


    public function register()
    {
        
        if($_SERVER["REQUEST_METHOD"] == "POST"){
            echo "i am inside register method of the register controller";
           $this->form_validation-> set_rules('first_name','First Name','trim|required|alpha');
           $this->form_validation-> set_rules('last_name','Last Name','trim|required|alpha');
           $this->form_validation-> set_rules('email','Email ID','trim|required|valid_email|is_unique[users.email]');
           $this->form_validation-> set_rules('password','Password','trim|required|mdS');
           $this->form_validation-> set_rules('cpassword','Confirm Password','trim|required|matches[password]');


       if($this->form_validation->run()==False)
       {
           //failed
           $this->index();
       } else {
           echo "I am here"; 
          }
       }
        return 'Only POST Method Allowed';
    }
}
?>