title: how to create like button using jquery, ajax and codeigniter
body: <blockquote>
    <p><strong>View</strong></p>
  </blockquote>
  
  <p>I have tried to create like button here. And everytime i click like button it increases by 1. Here how do i put value from database using <code>foreach()</code> loop and feeding it in jquery</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  $(document).ready(function(){
    $("#like").click(function() {
      var id='1069347886434951';//this is not artist who likes
      var creation_id= 1;
    $.ajax({
      type: "post",
      url: "&lt;?php echo base_url(); ?&gt;creations/like_creation",
      data:'id='+id + '&amp;creation_id=' +creation_id,//after its split, the split function gives an array
      success: function(response){        
        try{
          if(response=='true'){ 
            var newValue = parseInt($("#like").text()) + 1;            
            $("#"+voteId+'_result').html(newValue);// adds the value to no of like on the client side
          }else{
            alert('Sorry Unable to update..');
          }
        }catch(e) {   
          alert('Exception while request..');
        }   
      },
      error: function(){            
        alert('Error while request..');
      }
     });
      });
    });
    &lt;/script&gt;
  </code></pre>
  
  <blockquote>
    <p><strong>Controller</strong>
     The controller inserts the value in database. If you put the value using postman it will return true using json upto ajax. I have tried it in postman and it does.</p>
  </blockquote>
  
  <pre><code>public function like_creation(){
      //$artist_id=$this-&gt;session-&gt;userdata('user_id');
      $artist_id=$this-&gt;input-&gt;post('id');
  
      //bring creation id from the database when fed using foreach loop
      $creation_id=$this-&gt;input-&gt;post('creation_id');
      //$up_like1 =0;
      $data1=array(
      'id'=&gt; $this-&gt;input-&gt;post('id'),//bring it from artist_infors
      'creation_id'=&gt;  $this-&gt;input-&gt;post('creation_id'),
      'artist_who_likes'=&gt; $artist_id ,
  
      );
  
      $query=$this-&gt;hbmodel-&gt;insert_like($data1);     
  
  
      $status= "true";
  
      echo $status;
  }
  </code></pre>
  
  <blockquote>
    <p><strong>Model</strong> </p>
  </blockquote>
  
  <pre><code> public function no_likes($artist_id, $creation_id)
  {
      $sql="SELECT count(like_id) as num from likes as l where id='$artist_id'  and creation_id= $creation_id";
      //artist id has to determine whether it is user himself or the one whom       he/she tries to follow
      $query=$this-&gt;db-&gt;query($sql);
      return $query-&gt;result();
  }
  </code></pre>
  
  <blockquote>
    <p>MySQL qUERY</p>
  </blockquote>
  
  <pre><code> CREATE TABLE IF NOT EXISTS `likes` (
  `like_id` int(11) NOT NULL AUTO_INCREMENT,
  `id` varchar(500) COLLATE utf16_bin DEFAULT NULL,//the artist id whose creation is fed
  `creation_id` int(11) DEFAULT NULL,
  `artist_who_likes` varchar(500) COLLATE utf16_bin DEFAULT NULL,
  PRIMARY KEY (`like_id`)
  ) ENGINE=InnoDB  DEFAULT CHARSET=utf16 COLLATE=utf16_bin AUTO_INCREMENT=29 ;
  </code></pre>