0

我正在使用 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 数组,字段值为空。

我检查了页面源和输入字段值 = ''。

然而,我仍然看到我在输入字段中提交的值。

刷新页面仍然会显示值。

怎么了?

4

1 回答 1

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;
    }
    }
    ?>

验证运行为 true 后,调用该方法,如下例所示:

  if($this->form_validation->run() === FALSE)
    {
    # . . .
    }
    else
    {
    $this->form_validation->resetpostdata();
    # load view & other stuff
    }
于 2013-07-16T09:58:09.663 回答