Hello,
I am developing a web site with codeigniter 2.1.3
I am using an ajax call to post data to my controller, but for some reason the data is not being sent.
I am using ajax calls all over the application and they all work well just not this one.
My view:
<div id="ajaxResult">
<div class="page-header"><h2>Review Comments</h2></div>
<div class="row-fluid">
<div class="span12">
<?php if(count($comments)): ?>
<table class="table">
<thead>
<tr>
<th>Author</th>
<th>Email</th>
<th>IP Address</th>
<th>Date</th>
<th>Comment</th>
<th>Approve</th>
</tr>
</thead>
<tbody>
<?php foreach($comments as $comment): ?>
<tr>
<td>
<?php echo $comment->comment_author; ?>
</td>
<td><?php echo $comment->comment_author_email; ?></td>
<td><?php echo $comment->comment_author_IP; ?></td>
<td>
<?php
$date = new DateTime($comment->comment_date);
$date = $date->format('d/m/Y');
echo $date;
?>
</td>
<td>
<?php echo $comment->comment_content; ?>
</td>
<td>
<?php
$approve = array(
'name' => 'comment_approved',
'class' => 'approve',
'value' => '1',
'data-commentid' => $comment->id
);
echo form_checkbox($approve);
?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php else: ?>
<p>No comments were found for review.</p>
<?php endif; ?>
</div>
</div>
</div>
<script>
$(function()
{
$('.approve').on('click', function()
{
var id = $(this).data('commentid'); alert(id);
$.ajax({
url: "<?php echo site_url('blog/admin/review_comment'); ?>",
type:'POST',
data: { comment_id: id },
success: function(data) { $('#ajaxResult').html(data); } // End of success function of ajax form
}); // End of ajax call
});
});
</script>
My controller function:
public function review_comment()
{
$this->load->helper('form');
$this->load->model('blog_comment_m');
if(isset($_POST['comment_id']))
{
$id = $this->input->post('comment_id');
$data['comment_approved'] = 1;
$this->blog_comment_m->save($data, $id);
$this->data['comments'] = $this->blog_comment_m->get_by(array('comment_approved'=>0));
$this->load->view('admin/review_comments', $this->data);
}
else
{
$this->data['comments'] = $this->blog_comment_m->get_by(array('comment_approved'=>0));
$this->data['subview'] = 'admin/review_comments';
$this->load->view('admin/_layout_main', $this->data);
}
}
I tried to alert the comment_id and it does give me a value of 1.
In the controller it never goes into the if(isset($_POST['comment_id']) statement.
Why is that?
Thanks