我正在使用Codeigniter,并且最重要的是我有Bonefire(这可能是问题吗?),问题是每次我想使用Codeigniters辅助工具来验证表单我的条件运行的第一个条件(FALSE)和在该函数validation_errors()
之上没有运行...就像我帮助这个帮助程序的库甚至没有加载一样,尽管在书中做了所有事情:
if ($this->form_validation->run() == FALSE)
{
echo $msg = validation_errors();
}
else
{
$this->load->user_model->insert($data);
echo $msg = "Registration successfull";
}
让我先发布我的表单(我按目的省略了内联样式和类):
<div class="" style="">
<h1 id="header" class="">Login/Register</h1>
<form action="/public/index.php/users/sportappregister" >
<div style=""><input id="email" type="text" name="email" value="email" style=""></div>
<div style=""><input id="pass" type="text" name="password" value="password" style=""></div>
<div style="" class=""><img class="" style="" src="<?php echo img_path(); ?>ikone/fb_login_icon.png" />Login with Facebook</div>
<div id="send" style="" class=""><input type="submit"> Submit </div>
<div id="cancel" style="" class=""> Cancel </div>
</form>
</div>
正如您可以从表单操作中读取的那样,我的控制器位于公共类“sportappregister”下的文件“users”中,class Users extends Front_Controller
作为usuall,最后我在这个类中创建自己的函数来处理表单这样:
public function sportappregister(){
$email= ($this->input->get("email"));
$pass = ($this->input->get("password"));
$data = array(
"email" => $email,
"password" => $pass );
// here I load my helper
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
// rules for my form
$this->form_validation->set_rules('email', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
if ($this->form_validation->run() == FALSE)
{
echo $msg = validation_errors();
}
else
{
$this->load->user_model->insert($data);
echo $msg = "Registration successfull";
}
}
答案 0 :(得分:0)
我会改变一些事情。阅读下面修订函数中的评论;
public function sportappregister()
{
// Load these first
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
// Now set the rules
$this->form_validation->set_rules('email', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
if ( $this->form_validation->run() == false )
{
echo validation_errors();
}
else
{
// Build the array after the form validation
$data = array(
'email' => $this->input->post('email'), // POST, not GET
'password' => $this->input->post('password')
);
// Load your model
$this->load->model('users_model');
if ( $this->users_model->insert($data) )
{
echo 'Registration successful';
}
else
{
echo 'Registration failed';
}
}
}
您还加载了表单助手,但您没有使用它。它使建筑形式变得更加容易。
http://ellislab.com/codeigniter/user-guide/helpers/form_helper.html
<?php echo form_open('users/sportappregister'); ?>
答案 1 :(得分:0)
使用CI表单标记,例如form_open()
form_close()
等来构建表单。
你可以查看This link
使用get for login表单会使您的应用程序不安全。
其余代码对我来说似乎没问题。 只需改变这个
$email= ($this->input->post("email")); //changed get to post in both
$pass = ($this->input->post("password"));