我正在处理一个小型表格,将数据汇总到数据库中的表格并不引人注目。我试图添加一些验证规则,但他们不工作我仍然是PHP和codeigniter的初学者,所以无法弄清楚为什么有人可以看看我的代码,并提前帮助我tnx。
查看
<html>
<head>
</head>
<body>
<?php echo validation_errors(); ?>
<?php echo form_open('http://localhost/Surva/index.php/info/credentials/'); ?>
<ul id="info">
<li><label for='name'>Name:</label><?php echo form_input('name')?>
</li>
<li><label for='second_name'>Second Name:<label> <?php echo form_input('second_name');?>
</li>
<li><label fro='phpne'>Phone:</label> <?php echo form_input('phone');?></li>
<li><label for='email'>Email: </label><?php echo form_input('email');?>
</li>
<li><?php echo form_submit('submit', 'Start survay!!' );?></li>
</ul>
<?php echo form_close();?>
</body>
</html>
控制器
<?php
class Info extends CI_Controller{
function index(){
$this->load->view('info_view');
}
function credentials()
{
$data = array(
'name' => $this->input->post('name'),
'second_name' => $this->input->post('second_name'),
'phone' => $this->input->post('phone'),
'email' => $this->input->post('email'),
);
$this->info_model->add_record($data);
}
function validation(){
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Name', 'required|alpha|xss_clean');
$this->from_validation->set_rules('second_name', 'required|alpha|xss_clean');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
if ($this->form_validation->run() == FALSE){
$this->load->view('info_view');
}else{
redirect('survaycontroller/index');
}
}
}
?>
我使用了codeigniter用户指南进行验证,在那里解释看起来非常容易,整个结构都是从指南中获取的,我很想知道问题是什么。
答案 0 :(得分:2)
你可以用一个控制器做到这一点。我测试它,它的工作原理!
班级名称test.php
控制器名称索引
function index(){
$this->load->library('form_validation');
$data['name'] = $this->input->post('name');
$data['second_name'] = $this->input->post('second_name');
$data['phone'] = $this->input->post('phone');
$data['email'] = $this->input->post('email');
if($this->input->post('submit')) {
$this->form_validation->set_rules('name', 'Name', 'required|alpha|xss_clean');
$this->form_validation->set_rules('second_name', 'Second Name', 'required|alpha|xss_clean');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
if ($this->form_validation->run()){
$this->info_model->add_record($data);
}
}
$this->load->view('test');
}
查看:test.php
<html>
<head>
</head>
<body>
<?php echo validation_errors(); ?>
<?php echo form_open('test/index'); ?>
<ul id="info">
<li><label for='name'>Name:</label><?php echo form_input('name')?>
</li>
<li><label for='second_name'>Second Name:<label> <?php echo form_input('second_name');?>
</li>
<li><label fro='phpne'>Phone:</label> <?php echo form_input('phone');?></li>
<li><label for='email'>Email: </label><?php echo form_input('email');?>
</li>
<li><?php echo form_submit('submit', 'Start survay!!' );?></li>
</ul>
<?php echo form_close();?>
</body>
</html>
答案 1 :(得分:1)
您没有将表单发送到方法验证
查看强>
<html>
<head>
</head>
<body>
<?php echo validation_errors();
//you must redirect the form to the right method, in this case your method is "validation" on the controller is "info"
//another thing, you don't need to put all the url, just put the controller and the method, this way when you migrate your website to a server you don't have to worry changing the url
echo form_open('info/validation'); ?>
<ul id="info">
<li><label for='name'>Name:</label><?php echo form_input('name')?>
</li>
<li><label for='second_name'>Second Name:<label> <?php echo form_input('second_name');?>
</li>
<li><label fro='phpne'>Phone:</label> <?php echo form_input('phone');?></li>
<li><label for='email'>Email: </label><?php echo form_input('email');?>
</li>
<li><?php echo form_submit('submit', 'Start survay!!' );?></li>
</ul>
<?php echo form_close();?>
</body>
</html>
控制器
<?php
class Info extends CI_Controller{
//I've added the public before function
public function index(){
$this->load->view('info_view');
}
//In this one I've added private, Why? Because you don't peopple to use this method if they go to http://(yourdomain). This way you can only use this function inside this controller
private function credentials()
{
$data = array(
'name' => $this->input->post('name'),
'second_name' => $this->input->post('second_name'),
'phone' => $this->input->post('phone'),
'email' => $this->input->post('email'),
);
$this->info_model->add_record($data);
}
//this one is also as public, and it's the one who we'll receive the post request from your form
public function validation(){
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Name', 'required|alpha|xss_clean');
$this->from_validation->set_rules('second_name', 'required|alpha|xss_clean');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
if ($this->form_validation->run() == FALSE){
$this->load->view('info_view');
}else{
//if the form is valid then you call the private function credentials and save the data to your database
$this->credentials();
redirect('survaycontroller/index');
}
}
}
?>
检查我对您的代码所做的更改,如果您需要更多解释或帮助您
答案 2 :(得分:0)
第二个规则中缺少第二个参数
$this->from_validation->set_rules('second_name', 'Second Name', 'required|alpha|xss_clean');
答案 3 :(得分:0)
请看下面的示例,您必须在凭证()函数中检查$ this-&gt; form_validation-&gt; run()。
public function add_category()
{
$this->load->library('form_validation');
$data = array();
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
//Validation rules
$this->form_validation->set_rules('name', 'Name', 'trim|required');;
$this->form_validation->set_rules('description', 'Description', 'trim|required');
$this->form_validation->set_rules('position', 'Position', 'trim|numeric');
$artist_category = $this->input->post('artist_category', TRUE);
$data['artist_category'] = $artist_category;
if($this->form_validation->run() === FALSE)
{
$this->template->write('title', 'Category : Add Category');
$this->template->write_view('content', 'category/add_category',$data);
$this->template->render();
}
else
{
$name = trim($this->input->post('name',TRUE));
$description = trim($this->input->post('description',TRUE));
$position = trim($this->input->post('position',TRUE));
$colour = trim($this->input->post('colour',TRUE));
$language_id = trim($this->input->post('language_id',TRUE));
//Add record to categories table
$category_id = $this->Category_model->insert_category($name,$description,$position,$colour,$date_added);
$this->session->set_flashdata('success_msg', 'Category added successfully.');
//Redirect to manage categories
redirect('category');
}
}
else
{
$this->template->write('title', 'Category : Add Category');
$this->template->write_view('content', 'category/add_category');
$this->template->render();
}
}