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

3
  • 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? Commented Jan 24, 2019 at 6:45
  • 1
    call controller from outside CI-app: stackoverflow.com/questions/12257538/… Commented Jan 24, 2019 at 17:18
  • Sorry bro its my coding mistake, when i m changing submit button code from <input type="submit" value="Submit"> to <input type="submit" name="login" value="login"> it works fine Commented Jan 28, 2019 at 11:19

1 Answer 1

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

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

Comments

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.