Hello,
I am developing a site with CI 2.1.3
I have a blog module, in this blog I have a form to post a comment.
I am calling this form inside a view with:echo Modules:: run('blog/comment');
When I submit this form with ajaxForm, or refresh the page, the values of the input fields are not being cleared.
My controller’s function for the 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 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>
I dumped the $new_comment array and the fields values are empty.
I checked the page source and the input fields values = ''.
Yet, I still see the values that I submitted in the input fields.
Why is that?