[英]Controller not being called from view in codeigniter
我正在尝试使用codeigniter创建登录页面。 我有3个文件login_view.php,login.php,login_model.php
这是我的看法。
<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?> <!DOCTYPE html> <html> <body> <div class="container" style="margin-top:25px"> <div class="row"> <div class="col-md-6 col-sm-8 col-sm-push-2 col-md-push-3 col-xs-10 col-xs-push-1 header"> <img src="<?php echo base_url();?>img/dtulogo.png"/> </div><!-- /col --> <h1>University Registration System</h1> </div><!-- /row --> </div> <div class="container"> <div class="row"> <div class="form col-md-6 col-sm-8 col-sm-push-2 col-md-push-3 col-xs-11 "> <ul class="tab-group"> <li class="tab active"><a href="">Member</a></li> </ul> <!-- <div class="tab-content"> --> <div id="student"> <?php echo validation_errors(); ?> <form action="<?php echo base_url();?>login/member_login_handle" method="post"> <div class="field-wrap"> <input type="text" placeholder="MEMBER USERNAME" id="member_username" name="member_username" required autocomplete="off"/> </div> <div class="field-wrap"> <input type="password" PLACEHOLDER="MEMBER PASSWORD" id="member_password" name="member_password" required autocomplete="off"/> </div> <input type="hidden" name="<?php echo $this->security->get_csrf_token_name(); ?>" value="<?php echo $this->security->get_csrf_hash();?>" /> <button type="submit" class="button button-block"/>Log In</button> </form> </body> </html>
这是控制器文件
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Login extends CI_Controller { public function __construct() { parent::__construct(); $this->load->helper('url'); $this->load->library('form_validation'); $this->load->library(array('session')); } public function member_login_handle() { $this->load->helper('form'); $this->load->library('form_validation'); echo "<script>alert('in the member function');</script>"; $this->form_validation->set_rules('member_username', 'Memeber Username', 'trim|required'); $this->form_validation->set_rules('member_password', 'Password', 'trim|required'); if($this->form_validation->run() == FALSE){ $this->index(); } else{ $result = _check_database(); echo "<script>alert('$result');</script>"; } } public function _check_database() { echo "<script>alert('in the database function');</script>"; $member_username = $this->input->post('member_username', TRUE); $password = $this->input->post('member_password', TRUE); $this->load->model('login_model'); $result = $this->login_model->validate_member_login($member_username, $password); if($result){ return true; } else{ return false; } } public function index() { $this->load->view('login_view'); } }
视图页面没有调用控制器页面。 我还在控制器页面中添加了一些调试代码作为警报框。 但是它没有执行。 请查看到此代码中的问题。
对于初学者-您的控制器中有这个
$result = _check_database();
由于它是类中的一种方法,因此您需要使用$ this->来引用它,就像其他地方一样……
变成...
$result = $this->_check_database();
那将使您更进一步。
对于您的Debug,您可能需要更改此内容...(不确定js警报是否会显示布尔值...)
else{
$result = $this->_check_database();
echo "<script>alert('$result');</script>";
}
因此,要将布尔值翻译成单词,请尝试这样的操作...
else{
$result = $this->_check_database();
$debug_result = $result ? 'TRUE':'FALSE'; // Translate Boolean to a string
echo "<script>alert('$debug_result');</script>";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.