我正在使用CI 2.1.3开发一个网站
我有一个博客模块,在这篇博客中我有一个发表评论的表格。
我在一个视图中调用此表单:
echo Modules:: run('blog/comment');
当我使用ajaxForm提交此表单时,输入字段的值不会被清除。
我的控制器的评论表格功能:
public function comment($postId)
{
$this->load->helper('form');
$this->data['success'] = FALSE;
$this->data['postId'] = $postId;
if(!isset($_POST['comment_submit']))
{
$this->data['new_comment'] = $this->blog_comment_m->get_new();
}
else
{
$this->data['new_comment'] = $this->blog_comment_m->object_from_post(array('author', 'authur_email', 'content'));
$this->load->library('form_validation');
$rules = $this->blog_comment_m->rules;
$this->form_validation->set_rules($rules);
if($this->form_validation->run() == TRUE)
{
$this->data['success'] = TRUE;
$this->data['new_comment'] = $this->blog_comment_m->get_new();
}
}
$this->load->view('add_comment', $this->data);
}
评论表:
<div id="commentAjax">
<?php $attr = array('id'=>'commentForm'); echo form_open(site_url('blog/comment/' .
$postId), $attr); ?>
<input type="hidden" name="post_id" value="<?php echo $postId; ?>" />
<div style="border-top:2px groove #930"><h4>Leave a Comment</h4></div>
<div class="control-group <?php if(form_error('author')) echo 'error'; ?>">
<label>Name *</label>
<?php echo form_input(array('name'=>'author', 'class'=>'input-large', 'value'=>set_value('author', $new_comment->author))); ?>
<span class="help-block"><?php echo form_error('author'); ?></span>
</div>
<div class="control-group <?php if(form_error('author_email')) echo 'error'; ?>">
<label>Email *</label>
<?php echo form_input(array('name'=>'author_email', 'class'=>'input-large', 'value'=>set_value('author_email', $new_comment->author_email))); ?>
<span class="help-block"><?php echo form_error('author_email'); ?></span>
</div>
<div class="control-group <?php if(form_error('content')) echo 'error'; ?>">
<label>Comment *</label>
<?php echo form_textarea(array('name'=>'content', 'value'=>set_value('content', $new_comment->content))); ?>
<span class="help-block"><?php echo form_error('content'); ?></span>
</div>
<div>
<?php echo form_submit('submit', 'Send Comment', 'class="btn btn-submit"');?>
<input type="hidden" name="comment_submit" value="1" />
</div>
<?php echo form_close(); ?>
<?php if($success): ?>
<div style="border:1px solid #666; background:#9F9; color:#000; margin-top:10px; width:50%; padding:5px; font-weight:bold">
<p>Thank you for your comment.</p>
<p>To avoid spam, your comment has been submitted for approval.</p>
<p><h2 class="highland">Highland Coffee Roastery</h2></p>
</div>
<?php endif; ?>
</div>
<script>
$(function()
{
var options = { target: '#commentAjax' };
$('#commentForm').ajaxForm(options);
});
</script>
我转储了$ new_comment数组,字段值为空。
我检查了页面源和输入字段values =''。
然而,我仍然看到我在输入字段中提交的值。
刷新页面仍会显示值。
有什么问题?
答案 0 :(得分:0)
我在Daniweb论坛上得到了答案:
好的,问题是set_value()不考虑验证是运行true还是false,通常是redirect(),因此POST数组会自动重置,这里我们可以通过扩展/ system / libraries强制执行此操作/Form_validation.php,创建/application/libraries/MY_Form_validation.php并粘贴它:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class My_Form_validation extends CI_Form_validation {
public function __construct()
{
parent::__construct();
}
public function resetpostdata()
{
$obj =& _get_validation_object();
foreach($obj->_field_data as $key)
{
$this->_field_data[$key['field']]['postdata'] = NULL;
}
return true;
}
}
?>
验证运行后,调用方法,如下例所示:
if($this->form_validation->run() === FALSE)
{
# . . .
}
else
{
$this->form_validation->resetpostdata();
# load view & other stuff
}