CodeIgniter Forums
How can i prevent user vote double - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: How can i prevent user vote double (/showthread.php?tid=63203)



How can i prevent user vote double - freddy - 10-07-2015

Hello guys, thanks for some help in the past.

I have some stuck, about giving voting product who came from user vote, i already success giving vote but realiaze that user cannot giving double vote,

here is js
Code:
$(document).on("click", "[href='#atas']", function() {
   var value = this.id; //this is id product
       $.ajax({
               type: "post",
               url: base_url +"welcome/rating",
               data: "value=" + value,
               success: function(){
                   alert('data has been inserted');
                   },
               error: function(){
                       alert('Sorry you already vote');
                  }
     
           })
});

Controller
Code:
public function rating() {
          //insert table user

        $id=$this->session->userdata('id_user'); //get id user from session
        $id_product=$this->input->post('value');
        $data=array('id_user'=>$this->session->userdata('id_user'),
                    'id_product_post'=>$this->input->post('value'),
                    );
        $this->model_front->rating($data);

        //update in table product post
        $this->db->set('product_akurat', 'product_akurat+1', FALSE);
        $this->db->where('id_product_post', $id_product);
        $this->db->update('li_product_post');


/*
$this->db->where('id_user',$id);
   $query = $this->db->get('li_product_post_rating');
    if ($query->num_rows() > 0){
      //  should giving error cause user already vote
  }
   else{
      //inserted  data
   }

   */
}

and this is my model
Code:
public function rating($data)
{
//untuk insert ke database
$this->db->insert('li_product_post_rating',$data);
}


I'm trying to prevent in controller don't know where should start ! Thanks


RE: How can i prevent user vote double - InsiteFX - 10-07-2015

Assign the user_id to the post_rating then when they vote on a post set the user_id that they had voted,
you can then check the user_id and post_rating to see if they had already voted for it.

You would need a pivot table for this to work.

id - rating_id - user_id

 


RE: How can i prevent user vote double - freddy - 10-07-2015

(10-07-2015, 05:25 AM)InsiteFX Wrote: Assign the user_id to the post_rating then when they vote on a post set the user_id that they had voted,
you can then check the user_id and post_rating to see if they had already voted for it.

You would need a pivot table for this to work.

id - rating_id - user_id

 

yes i know but hard to coding in js and controller, 


RE: How can i prevent user vote double - jLinux - 10-07-2015

Why dont you modify your model to add the user ID to the vote, and when it adds a new vote, look for a row with the user ID, if it exists, update it, if not, insert a new one - This would allow users to edit their votes too, which would be nice


RE: How can i prevent user vote double - freddy - 10-07-2015

(10-07-2015, 11:44 AM)jLinux Wrote: Why dont you modify your model to add the user ID to the vote, and when it adds a new vote, look for a row with the user ID, if it exists, update it, if not, insert a new one - This would allow users to edit their votes too, which would be nice

Thanks for your respond, i know the logic like that but how can i make it validations  if users already vote and doesn't vote alert in javascript, honestly i just success inserted without checking user already vote or not, so i make the title how can i prevent user cannot vote double !
will you guide me by using my code js, controller and model if you don't mind ? 


RE: How can i prevent user vote double - mwhitney - 10-08-2015

You could retrieve their existing vote when the page is loaded and set a flag to indicate to the JavaScript that they have already voted.

You also need to add some validation before the insert to check whether they've already voted. This is generally the perfect use for the is_unique validation rule, since you wouldn't want multiple entries in the table with the same 'id_user' value.

If you want them to be able to update their existing vote and not bother with the is_unique validation, you can modify the model's rating() method to use replace() or to do a quick $this->db->where('id_user', $data['id_user'])->get() to determine whether you should use an update() or an insert(). You could also return a value from the rating() method to indicate whether an update or insert was performed (or neither), which would allow you to determine whether you need to update your li_product_post table, since you appear to be maintaining some sort of count of the votes in that table.


RE: How can i prevent user vote double - PaulD - 10-08-2015

I thought the error: and success: clauses were for ajax events, such as did the ajax call work or did it time out, or no server response etc.

Actual error checking of the response functionality is still done within the success: part of the ajax response. After all, the ajax request was successful, it is just that what the user tried to do was not allowed.

Normally I respond with a string that starts with either 'Error: You have not provided enough information' for example or 'Success: Your vote was registered', and then you could add another response for  'Voted: You have already voted', or 'Login: Your login has expired' etc etc.

I do a substr check on the response to see if it starts with Error, or in this example Voted, or Login, and assume that if it is not any of these error responses then it was a success. You can then, within your js, do whatever you need to do depending on the result.

Code:
    var response = theResponse.substring(0,5);
    
    if (response == 'Error') {
        // do whatever here for a non ajax event error
        ...

    } else if (response == 'Login') {
        // do whatever here for login required
        ...

    } else if (response == 'Voted') {
        // do whatever here for already voted
        ...

    } else {
        // do whatever here for success
        ...

    }

Hope that helps,

Best wishes,

Paul.


RE: How can i prevent user vote double - freddy - 10-08-2015

(10-08-2015, 07:19 AM)PaulD Wrote: I thought the error: and success: clauses were for ajax events, such as did the ajax call work or did it time out, or no server response etc.

Actual error checking of the response functionality is still done within the success: part of the ajax response. After all, the ajax request was successful, it is just that what the user tried to do was not allowed.

Normally I respond with a string that starts with either 'Error: You have not provided enough information' for example or 'Success: Your vote was registered', and then you could add another response for  'Voted: You have already voted', or 'Login: Your login has expired' etc etc.

I do a substr check on the response to see if it starts with Error, or in this example Voted, or Login, and assume that if it is not any of these error responses then it was a success. You can then, within your js, do whatever you need to do depending on the result.


Code:
var response = theResponse.substring(0,5);

if (response == 'Error') {
// do whatever here for a non ajax event error
...

} else if (response == 'Login') {
// do whatever here for login required
...

} else if (response == 'Voted') {
// do whatever here for already voted
...

} else {
// do whatever here for success
...

}

Hope that helps,

Best wishes,

Paul.

Thanks paul for you guide, it works but on my way, can i ask something about your js cause i'm not too experts on it ? 
here is my js 
Code:
$(document).on("click", "[href='#atas']", function() {
   var value = this.id;
       $.ajax({
               type: "post",
               url: base_url +"welcome/ratingup",
               data: "value="+value,
                success: function(data)
                   {
                       alert(data);
                    }
          })
});

and controller 

Code:
public function ratingup() {
    $id_product=$this->input->post('value');
    $id=$this->session->userdata('id_user'); //get id user from session
    $this->db->where('id_user',$id);
    $this->db->where('id_product_post',$id_product);
    $query = $this->db->get('li_product_post_rating');
       if ($query->num_rows() > 0)
       {
                      echo "Anda sudah melakukan voting !";//it variable data
        }

    else{
            
            $this->db->trans_start();
            $this->model_front->ratingup(); //insert user who vote
            $this->model_front->updateratingup(); //insert user who vote
            $this->db->trans_complete();
                  if ($this->db->trans_status() === FALSE)
                    {
                    echo "Terjadi error, silahkan ulangi beberapa saat lagi!";  //it variable data
                    }
                    {
                        echo  "Terima kasih atas votingnya!"; //it variable data
                    }

            }

}
my question is how and where should i combine my code with your logic ? 

my script works to vote and not allowing users double vote, just want to know more about ajax and jquery, many thanks and +1 for you