0

I have a Login form in html template and want to call the codeigniter controller function on form submission. Where html page is outside the CI folder and the CI folder name is binary, Ctrl_signin is my controller and chkvalidatelogin is my function, is it possible?

HTML Form Code:

<form action="binary/Ctrl_signin/chkvalidatelogin" method="post">
  <input type="email" name="username" class="email" placeholder="Username" required="" />
   <input type="password" name="Password" class="password" placeholder="Password" required="" />
    <input type="submit" value="Submit">
  </form>

Ctrl_signin Controller Code:

function chkvalidatelogin(){

  if(isset($_POST['login']) && $_POST['login']=='login')
  {
    $username=$_POST['username'];
    $password = $_POST['password'];
    $data = $this->Mdl_signin->validatelogin('member',$username,$password);
    if($data>0)
    {
      $userdata=$this->Mdl_signin->fetchmemid($username);
      $mid=$userdata->mem_lid;
      $mid1=$userdata->username;
      $_SESSION['user'] = $mid1;
      $_SESSION['mlid'] = $mid;
      $_SESSION['username'] = $mid;
      $this->session->set_userdata('login','true');
      $msg['message']="successfully login";
      redirect(base_url().'Ctrl_signin/Dashboard',$msg);
    }
    else
    {
      $msg="login failed!!";
      redirect(base_url().'Ctrl_signin/signin?mesg='.$msg);
    }
  }
 }

Mdl_signin Model Code:

  function validatelogin($table,$mid,$password)
    {   
    $query=$this->db->query('select * from '.$table.' where username="'.$mid.'" and decrepted_password="'.$password.'"');
    return $query->num_rows();
    }
  function fetchmemid($username)
    {   
     $sql='select mem_lid,username from member where username="'.$username.'"';
     $query=$this->db->query($sql);
    return $query->row();
    }

I tried above code but it shows the blank page on function url

Community
  • 1
  • 1
Kunal Waghmare
  • 183
  • 3
  • 10
  • Calling a controller from a request (through URL in browser/form action etc) is pretty much what a controller is for so it sounds like you want to use the default functionallity of CI. Have you gone through [the manual about controllers?](https://www.codeigniter.com/user_guide/general/controllers.html?highlight=controller) – M. Eriksson Jan 24 '19 at 06:45
  • 1
    call controller from outside CI-app: https://stackoverflow.com/questions/12257538/call-controller-method-of-codeigniter-outside-application-directory – Vickel Jan 24 '19 at 17:18
  • Sorry bro its my coding mistake, when i m changing submit button code from to it works fine – Kunal Waghmare Jan 28 '19 at 11:19

1 Answers1

1

It is very possible. Just point the form action to the full URL including the controller and method such as example.com/controller/method

Note, however, that if your CSRF protection is enabled you may encounter the controller refuses to process the form. You'd need to disable CSRF on that specific controller/method (not really recommended), disable CSRF at all (absolutely not recommended) or figure out a way to comply with CI's CSRF validations from outside Codeigniter

Javier Larroulet
  • 3,047
  • 3
  • 13
  • 30