i developing site ci 2.1.3
i have blog module, in blog have form post comment.
i calling form inside view with:
echo modules:: run('blog/comment');
when submit form ajaxform, values of input fields not being cleared.
my controller’s function comment form:
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); }
the comment form:
<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 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 comment.</p> <p>to avoid spam, comment has been submitted 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>
i dumped $new_comment array , fields values empty.
i checked page source , input fields values = ''.
yet, still see values submitted in input fields.
refreshing page, still, displays values.
what wrong?
i got answer on daniweb forum:
ok, problem set_value() doesn’t consider if validation runs true or false, redirect() , post array resetted automatically, here can force action extending /system/libraries/form_validation.php, create /application/libraries/my_form_validation.php , paste this:
<?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 $key) { $this->_field_data[$key['field']]['postdata'] = null; } return true; } } ?>
after validation runs true, call method, in example:
if($this->form_validation->run() === false) { # . . . } else { $this->form_validation->resetpostdata(); # load view & other stuff }
Comments
Post a Comment