[英]Codeigniter:Helper functions are not loaded
我已經創建了一個表單,並且在提交表單時沒有在字段中輸入數據,而是在將表單助手加載到控制器中后,它重定向到空白頁面,而不是使用form_open()打開的頁面。 實際上我無法使用url helper的功能。我已經以另一種方式發布了此問題,但是沒有任何解決方案。 我已經嘗試了來自codeigniter文檔的以下代碼,但對我而言不起作用。
為什么會這樣。 請幫我解決。
控制器:
class Login extends CI_Controller{
function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
$this->form_validation->set_rules('email', 'Email', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('login_form');
}
else
{
$this->load->view('success');
}
}
}
login_form視圖:
<html>
<head>
<title>My Form</title>
</head>
<body>
<?php echo validation_errors(); ?>
<?php echo form_open('login'); ?>
<h5>Username</h5>
<input type="text" name="username" value="" size="50" />
<h5>Password</h5>
<input type="text" name="password" value="" size="50" />
<h5>Password Confirm</h5>
<input type="text" name="passconf" value="" size="50" />
<h5>Email Address</h5>
<input type="text" name="email" value="" size="50" />
<div><input type="submit" value="Submit" /></div>
</form>
</body>
</html>
成功視圖:
<html>
<head>
<title>My Form</title>
</head>
<body>
<h3>Your form was successfully submitted!</h3>
<p><?php echo anchor('form', 'Try it again!'); ?></p>
</body>
</html>
提前致謝...
$ this-> load-> helper('form','url');
而不是
$this->load->helper(array('form', 'url'));
應該可以。
編輯:不確定是什么問題,但是您是否嘗試在/application/config/autoload.php
加載幫助程序?
$autoload['helper'] = array('form', 'url');
如果您配置了.htaccess文件,則您的代碼似乎正確。 因為我嘗試了您的代碼,所以一切正常。當我提交空白表格時,給出了驗證錯誤。
.htaccess文件的代碼
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|(.*)\.swf|forums|images|css|downloads|jquery|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?$1 [L,QSA]
在__construct()
添加parent::__construct()
__construct()
嗎?
class Login extends CI_Controller{
public function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
....
在您的login
控制器中:
<?php
class Login extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function index() {
$data = array();
$data['title'] = 'Login Form';
$this->load->view('login_form', $data);
}
public function save() {
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
$this->form_validation->set_rules('email', 'Email', 'required');
if ($this->form_validation->run() != FALSE)
{
redirect('login/success');
}
else
{
$this->index();
}
}
public function success() {
$data = array();
$data['title'] = 'Successfully Submitted';
$this->load->view('success_view', $data);
}
}
?>
在您的登錄視圖頁面中更改表單操作
<?php echo form_open('login/save'); ?>
要么
<form method="post" action="<?php echo base_url(); ?>login/save">
注意:您可以在瀏覽器中運行登錄表單,例如http://localhost/project_folder/login/
可以嘗試創建一個構造類
確保文件名是login.php中第一個字母不僅是所有方式大寫笨ucfirst有時會引起問題,如果不喜歡做這里
class Login extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper('url');
$this->load->helper('form');
$this->load->library('form_validation');
}
function index() {
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
$this->form_validation->set_rules('email', 'Email', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('login_form');
}
else
{
$this->load->view('success');
}
}
}
嘗試添加這個
parent::__construct();
給你的構造函數
function __construct(){
parent::__construct();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.