0

I am designing a like and dislike feature. But problem arise when i update my like as the same time it must update the dislike div too.

this is my .js for dislike:

$('.dislike').click(function(e) {
    dislike = $(this);
    to_dislike= dislike.data('val');
    $.ajax({
        type     : 'POST',
        url      : '<?php echo site_url('/url.php');?>',
        data     : 'dislikes='+to_dislike,
        dataType : 'text',
        success  : function(data){
            $("#dislike_"+to_dislike).html(data);
        },
        error    : function(){
            $("#dislike_"+to_dislike).html('Sorry');
        }
    });
});

Its working good. but i want to modify it in such a way that it should simultaneously update my div:

<div id='like'></div>

Here is my HTML :

<table>
    <tr>
        <td> <button class="like"></button></td>
        <td> <button class="dislike"></button></td>
    </tr>
    <tr>
        <td>LIKE : <div id='like'></div></td>
        <td> Dislike : <div id='dislike'></div></td>
    </tr>
</table>

EDIT:

Update: I have to fetch data about like and dislike at same time and then put that data in two different div's. There is a table with like and dislike column.On like i update my likes. But if a person dislike after liking a particular post then dislike is updated to +1 and like is updated to -1.

please help..

Rahul
  • 1,181
  • 1
  • 11
  • 20
  • Not sure what you mean by "it should simultaneously update my div", can you provide a reproducible example of your issue? preferably in a [jsfiddle](http://jsfiddle.net) – omma2289 Oct 29 '13 at 23:45
  • need to be more detailed about what `update` means. Some html would help also – charlietfl Oct 29 '13 at 23:49
  • i have to fetch data about like and dislike at same time and then put that data in two different divs – Rahul Oct 29 '13 at 23:49
  • what's in your data and how is it structured... last comment doesn't tell us much – charlietfl Oct 29 '13 at 23:53
  • @charlietfl its the number(integer) of like and dislike about perticular post – Rahul Oct 29 '13 at 23:54
  • ok I have 2 numbers now.... still no idea what the issue is...or how that data is structured. You aren't making this easy for us to understand – charlietfl Oct 29 '13 at 23:55
  • @charlietfl there is a table with like and dislike column. on like i update my likes. But if a person dislike after liking a particular post then dislike is updated to +1 and like is updated to -1. I hope it helped – Rahul Oct 29 '13 at 23:58
  • ok so what do you need...how to parse the text in table...or just change it with new data? Back to ...a simple html demo would help – charlietfl Oct 29 '13 at 23:59
  • @charlietfl I would like to change both the div's with new data. But with one ajax call. – Rahul Oct 30 '13 at 00:01
  • isn't the data sent back in call you already make? – charlietfl Oct 30 '13 at 00:02
  • @charlietfl yes, data is sent about that particular post who is to updated – Rahul Oct 30 '13 at 00:04
  • are you expecting your code to do something in success that it doesn't ? – Mina Gabriel Oct 30 '13 at 00:05
  • @MinaGabriel i want one ajax call and update 2 different div's at success – Rahul Oct 30 '13 at 00:07
  • Rahul.... I suggest you start all over and provide html and sample data . We have no idea what you need without seeing both – charlietfl Oct 30 '13 at 00:08
  • so you don't have any issue updating your database you are saying that the issue is your success function update the status of one div and not the other ... right ? – Mina Gabriel Oct 30 '13 at 00:10
  • @MinaGabriel Yes dear i have to update my database too. – Rahul Oct 30 '13 at 00:14
  • I think this is not meant to be, but both your like and dislike divs have the same id (like) – Maen Oct 30 '13 at 00:16
  • @Bigood can you give me some idea how can we make this work that way – Rahul Oct 30 '13 at 00:19
  • You mean, with two divs with the same ID? [You shouldn't!](http://stackoverflow.com/questions/5611963/can-multiple-different-html-elements-have-the-same-id-if-theyre-different-types) – Maen Oct 30 '13 at 00:22
  • @Bigood no dear dislike are there in div with id="dislike" whereas like are also in div with seperate id="like" – Rahul Oct 30 '13 at 00:24
  • Ok, so correct your code in your question, and forget about it. – Maen Oct 30 '13 at 00:25
  • @Bigood oops that was typo. – Rahul Oct 30 '13 at 00:27

2 Answers2

2

If i am getting you right then u want to get data from database and display it in two different div using one jquery call.

Why don't you concatenate the string with some symbol like $ and then when echo it. On Success just filter the data and put it in different div. Hope it helped. :)

juhi
  • 21
  • 2
1

If I understand correctly you just want to update the number of likes and dislikes in your divs.

$('.dislike').click(function(e) {
   dislike = $(this);
   to_dislike= dislike.data('val');
   $.ajax({
       type     : 'POST',
       url      : '<?php echo site_url('/url.php');?>',
       data     : 'dislikes='+to_dislike,
       dataType : 'text',
       success  : function(data){
           var dislike = parseInt($("#dislike").html());
           var like = parseInt($("#like").html());
           if ( dislike >= 0 && like >= 0) {
              $("#dislike").html(dislike + 1);
              $("#like").html(like - 1);
           }
       }
   });
});
jj689
  • 456
  • 2
  • 4
  • I want to update that for sure but want the values to be coming from database.It just showing an updated value not really fetching updated value from database. +1 for trying. – Rahul Oct 30 '13 at 04:51
  • @Rahul what does the response from your server look like? – jj689 Oct 30 '13 at 05:50