pimpace
pimpace

Reputation: 307

updating a column in database using jQuery Ajax in codeigniter

I have a views column in my database. And I want to update the column with +1 when a hyperlink is clicked to visit a certain page. Am working with jQuery, Ajax and codeigniter. I an not able to do the correct thing so as to trigger update in the database. Please assist me with what am missing.

Below are my codes:

HTML

<a href="<?php echo base_url(); ?>site/details/<?php echo $value->hid; ?>" name="hid" id="clicker">

JS

$(document).ready(function(){

      $("a#clicker").click(function(){

        $.ajax({
          type: "POST",
          url: "<?php base_url(); ?>site/traffic",
          success: function(data){
            alert("I got a view");
            console.log(data);
          }
        });
      });
});

Controller

public function traffic(){
    $id = $this->input->post("hid");
    $this->My_model->updateView($id);
}

Model

public function updateView($id = NULL, $data){
    $this->db->set('view', 'view+1');
    $this->db->where('id', $id);
    $this->db->update($this->table, $data);
}

Upvotes: 1

Views: 8995

Answers (3)

qazi yaseen ul haq
qazi yaseen ul haq

Reputation: 11

You are not passing the second required parameter $data to updateView, which will cause an exception. Furthermore, $data is not needed in this instance as you are already setting the parameters to update inside $this->db->set().

Upvotes: 1

You're near to solve your problem.

First, in your JQuery Ajax code, I don't see the data parameter, which sets the Post data to be sent. In your case, you need to send to the PHP script the "hid" parameter, so you have to specify it this way:

<a href="<?php echo base_url(); ?>site/details/<?php echo $value->hid; ?>" name="hid" id="clicker" data-hid="<?php echo $value->hid; ?>">

Notice that I've added the "hid" to the attribute "data-hid" of the link. Now, we can retrieve the "hid" of the user clicked link in JQuery in order to send it to the PHP script.

$("a#clicker").click(function(e){
    var hidClicked = e.currentTarget.data("hid"); // retrieve the hid by data attr.
    $.ajax({
        type: "POST",
          url: "<?php base_url(); ?>site/traffic",
          data: { hid : hidClicked }, // pass it as POST parameter
          success: function(data){
            alert("I got a view");
            console.log(data);
         }
     });
 });

Now in your PHP code, you can retrieve the "hid" of the clicked element by

public function traffic(){
    $id = $this->input->post("hid"); // this will return the hid POST parameter
    $this->My_model->updateView($id);
}

Then, I don't know why are you declaring the "$data" parameter in the updateView method. If you want to update the database, you only need the "hid" of the clicked element. So the method will be

public function updateView($id = NULL){
    $this->db->set('view', 'view+1');
    $this->db->where('id', $id);
    $this->db->update("NAME_OF_YOUR_TABLE");
}

That would be my solution, maybe there is one better.

Upvotes: 1

Matt Peck
Matt Peck

Reputation: 24

You are not passing the second required parameter $data to updateView, which will cause an exception.

Furthermore, $data is not needed in this instance as you are already setting the parameters to update inside $this->db->set().

Example from CodeIgniter 3 Documentation:

$this->db->set('field', 'field+1');
$this->db->where('id', 2);
$this->db->update('mytable'); // gives UPDATE `mytable` SET `field` = 'field+1' WHERE `id` = 2

Updating Data - CodeIgniter 3 Docs

Upvotes: 0

Related Questions