如何从模式窗体登录操作调用控制器的方法?



目前我一直在尝试将应用程序连接到数据库,但特别是在登录表单上。我试图从登录模式表单操作中调用控制器的方法login_action,但每当我尝试提交用户名和密码时,都不会发生任何事,它只需关闭表单操作框。这是我的代码C:\examplep\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>

这是我的代码C:\examplep\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'));
}
}

这是我的C:\examplep\htdocs\portalap\index.php

<?php 
require_once 'config_folder.php';
header("Location: ".$config_folder["base_url"].$config_folder["default_akses"]);
exit;
//

这是我的文件夹结构,取自VS代码浏览器:https://prnt.sc/nPSFQKiz3KWK

我已经尝试将modals表单操作更改为action="<?php echo base_url('/welcome/login_action')?>",但它返回404未找到的页面。这是我的$config['base_url'] = 'http://localhost/portalapp/'。以防万一,这是我的C:\examplep\htdocs\portalap\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>

但是,如果我将表单操作更改为页面,而不是调用控制器的方法(如sdm-page(,它就可以工作了。但我想要的是调用Welcome控制器的方法login_action,但感觉它不会调用它。即使我在C:\examplep\htdocs\portalap\Welcome\application\controllers\Welcome.php中删除了login_action方法,当我提交用户名和密码时,它也不会给我带来任何错误。请帮帮我…怎么了?我该怎么做才能让它发挥作用?

您使用的是html<form>标记以及CI帮助器函数form_open(),它为您创建了一个嵌套的表单结构,但您不能这样做,请参阅:您能嵌套html表单吗?。

因此,请坚持其中一种方法,例如使用CI语法:

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

并删除这些行:

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

关于form_open()函数和相关语法(添加表单属性或隐藏字段(的更多信息,请点击此处CI.4x和此处CI3.x

注意1:在表单的操作中,没有index.php(因为您使用.htaccess删除了它(,也没有前导斜杠/

note2:正如我在评论中所写:使用md5进行密码哈希被认为是不安全的,请检查PHP密码的安全哈希和salt。使用password_hash((中内置的php代替

最新更新