1

Currently I am stuck on trying to connect the app to the database but specifically on the login form. I am trying to call controller's method called login_action from my sign in modals form action but whenever i try to submit the username and password nothing happens, it simply close the form action box. Here is my code of C:\xampp\htdocs\portalapp\welcome\application\views_partials\modals.php

<div class="modal fade" id="modalmasuk">
    <div class="modal-dialog modal-dialog-centered">
      <div class="modal-content">
        
      
      <img class="d-block w-100" src="<?php echo base_url('assets/images/bg_.jpg')?>">
    
      
        <div class="modal-body">
          
          <form class= "form-signin" action="<?php echo base_url('index.php/welcome/login_action')?>" method="post">
          <?php echo validation_errors();?>
          <?php echo form_open('index.php/welcome/login_action')?>
          <p class="text-center text-danger">
              <?php 
              if ($this->input->get('action')=='error'){
                  echo "Email atau Kata Sandi salah";
              } elseif ($this->input->get('action')=='notlog'){
                  echo "Anda belum masuk";
              }
              ?> 
            </p>
                          
            <div class="input-group mb-3">
              <input type="email" class="form-control" placeholder="Nama User" name="email" id="email">
              <div class="input-group-append">
                <div class="input-group-text">
                  <span class="fas fa-user"></span>
                </div>
              </div>
            </div>
            <div class="input-group mb-3">
              <input type="password" class="form-control" placeholder="Kata Sandi" name="password" id="password">
              <div class="input-group-append">
                <div class="input-group-text">
                  <span class="fas fa-lock"></span>
                </div>
              </div>
            </div>
            <div class="row">
              <div class="col-8">
                <div class="icheck-primary">
                  <input type="checkbox" id="remember">
                  <label for="remember">
                    Ingatkan Saya
                  </label>
                </div>
              </div>
              <!-- /.col -->
              <div class="col-4">
              <a tabindex="0" class="btn btn-xs" role="button" data-toggle="modal" data-target="#modalLupa">Lupa Kata Sandi</a>
              
              </div>
              <!-- /.col -->
            </div>
            <div class="modal-footer justify-content-between">
            <input class = "btn btn-lg btn-success btn-block" type="submit" name="submit" value="Masuk">
                
              </div>
                
    
              <!-- /.col -->
            </div>
            <?php echo form_close()?>        
          </form>

        </div>

      </div>
      <!-- /.modal-content -->
    </div>
    <!-- /.modal-dialog -->
  </div>
  <!-- /.modal -->


  <div class="modal fade" role="dialog" id="modalLupa" style="z-index: 100000;">
<div class="modal-dialog" style="width: 380px;margin-top: 20%">
    <div class="modal-content bg-warning"  >
        <div class="modal-body">
            <div id="lupa_sandi">
                <div class="ct_lupa">
                    <b>Lupa kata sandi : </b>
                    
                       Harap hubungi administrator
                    
                </div>
            </div>
        </div>
    </div>
</div>

Here is my code of C:\xampp\htdocs\portalapp\welcome\application\controllers\Welcome.php

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

class Welcome extends CI_Controller {
    function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form','url'));
        $this->load->library('form_validation');
        $this->load->library('session');
        $this->load->model('m_welcome');
        
    }

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     *      http://example.com/index.php/welcome
     *  - or -
     *      http://example.com/index.php/welcome/index
     *  - or -
     * Since this controller is set as the default controller in
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see https://codeigniter.com/user_guide/general/urls.html
     */
    public function index()
    {
        $this->load->view('landing');
    }

    function login_action(){
        $email = $this->input->post('email');
        $password = $this->input->post('password');
        $where=array(
            'email'=>$email,
            'password'=>md5($password)
        );

        if ($this->m_welcome->check_login($where)->num_rows() > 0){
        $data_session=array(
            'nama'=>$email,
            'status'=>'login'
        );
            
        $this->session->set_userdata($data_session);

        redirect(base_url("sdm"));

        } else {
            redirect('welcome/?action=error');
        }
    }

    function logout_action(){
        $this->session->session_destroy();
        redirect(base_url('login'));
    }
}

This is my C:\xampp\htdocs\portalapp\index.php

<?php 

require_once 'config_folder.php';

 header("Location: ".$config_folder["base_url"].$config_folder["default_akses"]);
exit;
//

And this is my folder structure taken from VS Code Explorer: https://prnt.sc/nPSFQKiz3KWK

I have already tried to change the modals form action to action="<?php echo base_url('/welcome/login_action')?>" but it returns 404 page not found. This is my $config['base_url'] = 'http://localhost/portalapp/'. Just in case, this is my C:\xampp\htdocs\portalapp\welcome.htaccess

<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php/$1
</IfModule>

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.
    # Submitted by: ElliotHaughin

    ErrorDocument 404 /index.php
</IfModule>

But if i change the form action to a page instead of calling controller's method such as sdm page, it works. But what i want is calling the Welcome controller's method login_action, however it feels like it won't call it. Even if i delete the login_action method in C:\xampp\htdocs\portalapp\welcome\application\controllers\Welcome.php , when i submit username and password it won't throw me any error. Please help me.. What went wrong? What should i do to make it works?

Vickel
  • 7,879
  • 6
  • 35
  • 56
  • 1
    in addition to your question: using md5 for password hash is considered unsafe, check [Secure hash and salt for PHP passwords](https://stackoverflow.com/questions/401656/secure-hash-and-salt-for-php-passwords). Use php built in [password_hash()](https://www.php.net/manual/en/function.password-hash.php) instead – Vickel Mar 01 '22 at 16:40
  • cool, thanks for the heads up – Codeboy Newbie Mar 02 '22 at 02:27

1 Answers1

0

you are using the html <form> tag as well as the CI helper function form_open(), which creates you a nested form structure, which you cannot do, see: Can you nest html forms?.

So stick to either one of the approaches, e.g. going with the CI syntax:

<?php echo validation_errors();?>
<?php echo form_open('welcome/login_action')?>
... more code ...
<?php echo form_close()?> 

and remove these lines:

<form class= "form-signin" action="<?php echo base_url('index.php/welcome/login_action')?>" method="post">
</form>

more about the form_open() function and related syntax (adding form attributes or hidden fields) here CI.4x and here CI3.x

note1: in the form's action, there is no index.php (since you removed that with your .htaccess) and also no leading slash /

note2: as I wrote in my comment: using md5 for password hash is considered unsafe, check Secure hash and salt for PHP passwords. Use php built in password_hash() instead

Vickel
  • 7,879
  • 6
  • 35
  • 56
  • I tried to remove those two lines but the result still the same, i also tried to use the CI helper function instead of the html form but still nothing happens after i submit the username and password – Codeboy Newbie Mar 02 '22 at 02:27
  • go by steps, you need to debug this! make sure ENVIRONMENT is set to development and check your browser/server for errors. Reduce all the code inside the form to just one input field (e.g. email) and check with `die(print_r($_POST)`in the controller if you are getting there – Vickel Mar 02 '22 at 02:33