1,105,578 Community Members

.ajax post does not send the data to my php file

Member Avatar
cgull
Junior Poster
123 posts since Dec 2009
Reputation Points: 25 [?]
Q&As Helped to Solve: 5 [?]
Skill Endorsements: 2 [?]
 
0
 

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

Member Avatar
Atli
Practically a Master Poster
691 posts since May 2007
Reputation Points: 182 [?]
Q&As Helped to Solve: 107 [?]
Skill Endorsements: 16 [?]
 
0
 

In CodeIgniter you really shouldn't be doing if (isset($_POST["comment_id"])). Instead you should just be doing

$id = $this->input->post("comment_id");
if ($id) {
    // Do stuff if the id was sent.
}
else {
    // Do stuff if the id wasn't sent.
}

The CodeIgniter input class handles the request arrays for you, so all you have to do is call it like that, and it'll return either the value or a FALSE.

Personally I'm not a fan of this approach, but that's how the CI people like to do it, so that's how you should do it within their framework.

Member Avatar
cgull
Junior Poster
123 posts since Dec 2009
Reputation Points: 25 [?]
Q&As Helped to Solve: 5 [?]
Skill Endorsements: 2 [?]
 
0
 

Thanks Atli, but that didn't help. $this->input->post("comment_id") is the same as $_POST['comment_id']. I don't think it matters if I use the ci function or not.

I tried your way anyway, and the $id remains blank.

I don't understand what is going on. Works fine everywhere else I use an ajax call in the application.

Member Avatar
Atli
Practically a Master Poster
691 posts since May 2007
Reputation Points: 182 [?]
Q&As Helped to Solve: 107 [?]
Skill Endorsements: 16 [?]
 
0
 

Have you looked at the AJAX request in the browser's dev tools, just to see if everything is in order with the actual request?

Member Avatar
cgull
Junior Poster
123 posts since Dec 2009
Reputation Points: 25 [?]
Q&As Helped to Solve: 5 [?]
Skill Endorsements: 2 [?]
 
0
 

Thanks Atli, but that didn't help. $this->input->post("comment_id") is the same as $_POST['comment_id']. I don't think it matters if I use the ci function or not.

I tried your way anyway, and the $id remains blank.

I don't understand what is going on. Works fine everywhere else I use an ajax call in the application.

Member Avatar
Atli
Practically a Master Poster
691 posts since May 2007
Reputation Points: 182 [?]
Q&As Helped to Solve: 107 [?]
Skill Endorsements: 16 [?]
 
0
 

Yea, I got that the first time. What I was asking is whether or not you have checked that the AJAX request is being sent correctly, using dev tools like Firebug.

According to your first post, the id value passed to your jQuery AJAX call is showing fine when printed/alerted before the call, but that same value is not being received correctly by PHP. The logical conclusion is that there is something off with the AJAX request. So, debugging the request seems like the thing to do, wouldn't you agree?

If you are unfamiliar with these browser dev tools, I suggest you look into them. They are essential when debugging JavaScript code. All the browsers have them these days.

Member Avatar
cgull
Junior Poster
123 posts since Dec 2009
Reputation Points: 25 [?]
Q&As Helped to Solve: 5 [?]
Skill Endorsements: 2 [?]
 
0
 

Sorry Atli, didn't see your other answer. Thank you for trying to help again.
If I look at the NET tab in firebug I see this: Status: 302 error found (Read about that, I am not redirecting anything not sure why I get this error.
In the Post tab I see this:
Parameters comment_id = 1
Source comment_id = 1 So it all looks fine to me? Is there anything else I should check? Thank you very much
I also went to the url directly and the url works fine.

Someone in stackoverflow suggested it might be something in my htaccess file but I don't think so. My htaccess file:
RewriteEngine On
RewriteCond $1 !^(index.php|assets|images|robots.txt|captcha)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

This is so frustrating..... :(

Member Avatar
cgull
Junior Poster
123 posts since Dec 2009
Reputation Points: 25 [?]
Q&As Helped to Solve: 5 [?]
Skill Endorsements: 2 [?]
 
0
 

Sorry, another thing, the url is: mysite/admin/blog/review_comment
In my routes file I have this:
$route['blog/(:any)/(:num)'] = 'blog/$1/$2';
$route['blog/admin/$1/$2'] = 'blog/admin/$1/$2';

I tried without: $route['blog/(:any)/(:num)'] = 'blog/$1/$2';
didn't help

I tried without: $route['blog/admin/$1/$2'] = 'blog/admin/$1/$2';
didn't help

I tried without both, didn't help.

Member Avatar
cgull
Junior Poster
123 posts since Dec 2009
Reputation Points: 25 [?]
Q&As Helped to Solve: 5 [?]
Skill Endorsements: 2 [?]
 
0
 

OHHHH ! I FOUND IT!!!!

Oh dear, oh dear, it was the fact that I started using csrf on form submits, a new thing for me, and I didn't add this url to the config file.

Adding this:
$config['csrf_exclude_uris'] = array('blog/admin/review_comment');
Solved the problem......

OHHHHHHHHHHHHHHHHHHHHHH :)

Thank you very very much for all the help.

Question Answered as of 9 Months Ago by Atli
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: