Welcome Guest, Not a member yet? Register   Sign In
How can i prevent user vote double
#8

(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
Reply


Messages In This Thread
How can i prevent user vote double - by freddy - 10-07-2015, 01:36 AM
RE: How can i prevent user vote double - by PaulD - 10-08-2015, 07:19 AM
RE: How can i prevent user vote double - by freddy - 10-08-2015, 06:23 PM



Theme © iAndrew 2016 - Forum software by © MyBB