Php Codeigniter在运行form_validation->run()时有多个URL
我在views文件夹中有一个register_page.php文件,它只是一个注册表表单。当您单击register(注册)按钮并说密码不匹配时,在单击submit(提交)按钮后,应该会发现密码不匹配。但是,在您键入密码后,它仍然不匹配,它什么也不做,只是复制url 比如说 密码不匹配时的URL:http://localhost/dayone/user/register_user 密码仍然不匹配时的URL:http://localhost/dayone/user/user/register_user 此时,表单为空,所有值都被删除,当您按enter键而不填写任何内容时,不会显示任何错误,但URL显示:http://localhost/dayone/user/user/register_user 这是什么原因造成的 My user.php控制器:Php Codeigniter在运行form_validation->run()时有多个URL,php,codeigniter,Php,Codeigniter,我在views文件夹中有一个register_page.php文件,它只是一个注册表表单。当您单击register(注册)按钮并说密码不匹配时,在单击submit(提交)按钮后,应该会发现密码不匹配。但是,在您键入密码后,它仍然不匹配,它什么也不做,只是复制url 比如说 密码不匹配时的URL:http://localhost/dayone/user/register_user 密码仍然不匹配时的URL:http://localhost/dayone/user/user/register_use
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends CI_Controller {
public function index()
{
$this->load->view('includes/header');
$this->load->view('register_page');
$this->load->view('includes/footer');
}
public function register_user () {
$this->load->library('form_validation');
//rules to become a registered user
$this->form_validation->set_rules('first_name', 'First Name', 'required|trim|min_length[3]|max_length[20]|xss_clean');
$this->form_validation->set_rules('last_name', 'Last Name', 'required|trim|min_length[3]|max_length[20]|xss_clean');
$this->form_validation->set_rules('email', 'Email', 'required|trim|min_length[6]|max_length[50]|valid_email|is_unique[users.email]|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'required|trim|min_length[6]|max_length[50]|matches[password_conf]|xss_clean');
$this->form_validation->set_rules('password_conf', 'Confirm Password', 'required|trim|min_length[6]|max_length[50]|xss_clean');
if ($this->form_validation->run() == FALSE) {
//user didn't validate, send back to login form and show errors
$this->load->view('includes/header');
$this->load->view('register_page');
$this->load->view('includes/footer');
} else {
//successful registration
$this->load->view('login');
}
}
}
我不知道这是什么原因。而不是这个原因:
<form class="contact" id="contact-form" name="contact-form" method="post" action="user/register_user">
<?php echo form_open('user/register_user');?>
我建议你花点时间阅读更多关于CodeIgniter的用户指南
编辑:
不要忘记加载表单和url帮助程序。您可以在application/config的autoload.php中加载它们
$autoload['helper'] = array("url","form");
或
制作一个函数uu构造并在那里加载您的助手和模型
例如:
function __construct()
{
parent::__construct();
$this->load->model('sample_model');
$this->load->helper('url');
$this->load->helper('form');
}
如果需要设置一些默认值或运行
实例化类时的默认进程。构造函数不能
返回一个值,但它们可以执行一些默认工作
-来自类下构造函数
首先使用表单帮助器打开表单
<?php echo form_open('user/register_user');?>
那么您的else部分应该是重定向,而不是像这样再次加载视图
else {
// success register
redirect('login');
}
当你不使用echo form_open时,为什么还要麻烦使用echo form_close呢?在我看来,使用form_open可以正确创建表单,包括隐藏的CSRF字段。通常在CodeIgniter中,如果表单数据中没有包含正确的CSRF令牌值,则无法提交表单。对不起,我是Php/CodeIgniter新手。但是我如何替换代码并实现form_open呢?从这里开始:在form action中使用echo base_url'user/register_user'或form_open'user/register_user'。在使用之前,不要忘记加载url帮助程序。还可以在password_conf字段中使用匹配项来匹配密码
function __construct()
{
parent::__construct();
$this->load->model('sample_model');
$this->load->helper('url');
$this->load->helper('form');
}
<?php echo form_open('user/register_user');?>
$this->form_validation->set_rules('password', 'Password', 'required|trim|min_length[6]|max_length[50]|xss_clean');
$this->form_validation->set_rules('password_conf', 'Confirm Password', 'required|trim|min_length[6]|max_length[50]|matches[password]|xss_clean');
else {
// success register
redirect('login');
}