0

I am updating the database whenever a user clicks the like button. The update is successfully done but the problem is in updating the new fetched value from the database.

Control Function on which ajax is posting data :

public function plusrepo()
{
    if($this->input->is_ajax_request())
    {
        $this->load->model('themodel');
        $rep['updated'] = $this->themodel->addrepo($this->input->post('resid'));
        echo $rep['updated'][0]." <span>Reputation</span>";
    }
}

This is how i am selecting from table and returning the result array.

$this->db->select('repo');
$this->db->from('restaurants');
$this->db->where('id', $id);
$result = $this->db->get();
return $result->result_array();

My Ajax function on success does this :

success: function(){
         alert("Success");
         $(this).addClass('.up_arrow').removeClass('.up_arrowed');
         $('.rep_count').html(data);
         }

What is wrong? I'm confused.

Edited This is the complete function for ajax

$('.up_arrow').each(function() {
    $(this).click(function(event) {
        event.preventDefault();
        var resid = $(this).attr('name');

        var post_data = {
            'resid' : resid,
            '<?php echo $this->security->get_csrf_token_name(); ?>' : '<?php echo $this->security->get_csrf_hash(); ?>'
        };

        if(resid){
            $.ajax({
                type: 'POST',
                url: "/ci_theyaw/restaurants/plusrepo",
                data: post_data,
                success: function(data){
                    console.log(data);
                    // alert(data);
                    // $(this).addClass('.up_arrow').removeClass('.up_arrowed');
                    // $('.rep_count').html(data);
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    alert(xhr.status);
                    console.log(xhr.responseText);
                    alert(thrownError);
                }
            });
        }
    });
});
14
  • Is your current problem coming from your server side or your front end? Does the .success handler gets called (ie your alert() gets called)? Or does your controller echo the right data? It is best to isolate the problem. Commented Sep 18, 2013 at 11:58
  • No the alert is not getting called. The problem is i think on the server side. Commented Sep 18, 2013 at 12:02
  • How can i check what my controller just echoed? Commented Sep 18, 2013 at 12:02
  • If you are using Chrome you can open your console (F12 on Windows), go to Network tab, and see the request/response. Click on your response which will show up as soon as you click the button that initiates AJAX call, and check the 'Preview' Commented Sep 18, 2013 at 12:06
  • I did that and i'm getting an error right there POST http://localhost/ci_theyaw/restaurants/plusrepo jquery.js:8526 send jquery.js:8526 jQuery.extend.ajax jquery.js:7978 (anonymous function) restaurants:456 jQuery.event.dispatch jquery.js:3074 elemData.handle Commented Sep 18, 2013 at 12:07

3 Answers 3

1

You need to pass data to success function like this :

success: function(data){

So full ajax success callback function would be :

success: function(data){
     alert("Success");
     $(this).addClass('.up_arrow').removeClass('.up_arrowed');
     $('.rep_count').html(data);
}

Also :

$this->db->select('repo');
$this->db->from('restaurants');
$this->db->where('id', $id);
$this->db->limit(1);
$result = $this->db->get();
print_r($result);//For testing
//echo $result['repo']; // For working code
Sign up to request clarification or add additional context in comments.

5 Comments

did this still no luck?
what do you get while alert(data); ?
The error part of my ajax function is returning the alert(xhr.status) or alert(thrownError) as 0.
@Cruze : Try altering the php section as i did and also alerting the data obtained. Also try alerting resid
The problem is that success function is not getting executed and every time the xhr.status returns 0 which is in the error function. I think the server is not able to generate the response which it should.
1

Change,

$this->db->select('repo');
$this->db->from('restaurants');
$this->db->where('id', $id);
$result = $this->db->get();
return $result->result_array();

To,

$this->db->select('repo');
$this->db->from('restaurants');
$this->db->where('id', $id);
$result = $this->db->get();
$row=$result->result_array();
return $row['repo'];

And,

     success: function(){
       alert("Success");
       $(this).addClass('.up_arrow').removeClass('.up_arrowed');
       $('.rep_count').html(data);
     }

To,

     success: function(data){
       alert("Success");
       $(this).addClass('.up_arrow').removeClass('.up_arrowed');
       $('.rep_count').html(data);
     }

Here you forgot to pass data to success function.

7 Comments

I think this will return what i need but still no luck with the updating on page.
Yes noticed that but still the error part of my ajax is returning 0 as xhr.status / thrownerror
Try to log your data to console using console.log(data) from success function. What is the output?
error: function (xhr, ajaxOptions, thrownError) { alert(xhr.status); console.log(xhr.responseText); alert(thrownError); }
This handles the error.. so i'm just getting 0 as an alert. Success part isnt executing.
|
1

Try:

$this->db->select('repo');
$this->db->from('restaurants');
$this->db->where('id', $id);
$result = $this->db->get()->row_array();
return $result['repo'];

echo $rep['updated']." <span>Reputation</span>";

Change :

url: "<?=base_url('restaurants/plusrepo')?>",

1 Comment

Edited the above answer try once changing it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.