好的,我正在尝试在codeigniter中验证表单
作为第一步,我想让我的表格中的所有字段都需要输入... 但我不能让它发挥作用
这是代码:
myBlog.php
<?php
class MyBlog extends Controller{
function MyBlog(){
parent::Controller();
$this->load->helper('url'); //here we load a url class that we use later
$this->load->helper('form');// here we load form class
$this->load->scaffolding('entries'); //scaffolfing is a feature that lets you add or remove elements from the database
$this->load->library('form_validation');//load validation class used to validate our forms...
}
function index(){
$data['title'] = "My Blog Title"; //the title of my blog
$data['query'] = $this->db->get('entries'); //here we make a small query to entries table
$this->load->view('myBlog_view', $data); ///load all data variables on myBlog_view.php
//this is also for the form validation
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('body', 'Body', 'required');
$this->form_validation->set_rules('author', 'Author', 'required');
if ($this->form_validation->run() == FALSE)
{
//$this->load->view('myBlog_view');
}
else
{
$this->load->view('formSuccess_view');
}
}
function myBlog_insert(){
$this->db->insert('entries', $_POST);
redirect('myBlog/');
}
}
?>
myBlog_view.php文件:
<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body>
<?php foreach($query->result() as $row): ?>
<div class='curvebox'>
<h3><?=$row->title?></h3>
<p class="bodyText"><?=$row->body?></p>
<div class="author"><?="by: ".$row->author." on ".date("D d M Y h:i:s A", strtotime($row->date_time))?></div>
<p class="comments"><?=anchor('myBlog/comments/'.$row->id, 'Comments');?></p>
</div>
<?php endforeach; ?>
<div class="theForm">
<?php echo $this->form_validation->error_string; ?>
<?=form_open('myBlog/myBlog_insert');?>
<label for="title">Title:</label>
<input type='text' name="title" size="40" id="title" />
<p>
<label for="body">Body:</label>
<textarea name="body" rows = "10" cols="60" id="body"></textarea>
</p>
<p>
<label for="author">Author:</label>
<input type="text" name="author" size="40" id="author"/>
</p>
<p><input type="submit" value="Submit New Post"/></p>
</form>
</div>
</body>
</html>
一切正常,除了它接受带有半满的表格的条目
抱歉长代码.... 任何帮助将不胜感激
答案 0 :(得分:2)
您希望自己的表单发布。现在它正在跳过验证。
所有验证函数都在索引方法中。 在myBlog_view.php中更改此行:
<?=form_open('myBlog/myBlog_insert');?>
为:
<?=form_open('myBlog');?>
然后你需要在验证后调用insert方法,所以在你的控制器中,在这一行之前:$this->load->view('formSuccess_view');
你需要添加:
$this->myBlog_insert();
看看是否有效。