Visitor to rate with rating input once

Hello everybody and happy new year !
I have set a rating input element called ‘‘ratingsInput1’’ and rating display connected through a dataset to a collection. It is working but what I want is to let visitors rate only once.
I tried this code but it is sending an error telling it cannot read neither the ‘‘change’’ property nor the ‘‘disable’’ property.
Any help would be appreciated.
Thank you

$w.onReady( function () {
!local.getItem(“x”)
if ($w.ratingsInput1.change())
{local.setItem(“x”, “yes”)}
if (local.getItem(“x”, “yes”)){$w.ratingsInput1.disable()}
})

1 Like

The RatingsInput component does not have change or disable properties. See the RatingsInput API for more details on the properties and functions available.

I am interested if you have the code for this once function :slight_smile:

I was in the same situation where I wanted the users to rate once. My solution:

Save the userId of the person who is rating along with the itemId that is rated

Everytime someone wants to rate an item, run a check through the database to see if their userId and itemId are available together or not

function determinePrivilege(userId, $item, data) {
    wixData.query("RatedUsers")
    .eq("userId", userId)
    .eq("itemId", $item("#id").text)
    .find()

If the user has already rated then show them an error msg or lightbox in my case

If not then allow them to rate with a lightbox or any other way

Sometimes you need to be creative to get the job done if there is no direct solution available

7 Likes

Hello Shan,
This seems a nice solution, but how do I gather the userId’s in a database??? Thank you in advance.

See the Ratings by User example. It allows registered users to rate recipes when logged in. The user can only submit one rating, but can change it.

Thank you, Yisrael, but on my website people would vote without being registered. Looking for a solution for this, is about the star rating system that should be limited to a single vote.

It’s almost impossible to limit to one vote without having the users logging in. You can use local storage and save a created hash id for each visitor. Then when a user visits check the local storage and see if they’re a returning visitor. However, if they clear cookies/cache you’re back to zero.

The only way to be sure is to use a login. The Ratings example is simpler and you can try to adapt it to your needs.

@yisrael-wix Can you give me a hint on how to create a hash id dataset for each visitor? Thank you.

@ovidiup76 One way would be something like this:

visitorId = Math.random().toString(36);
local.setItem('visitor-id', visitorId);

Realize however that if the user clears cookies/cache then this disappears.

@yisrael-wix where should I insert this code? Thank yoi in advance, but I am a little confused.

Hey can you possibly explain how you got saved the userid into the dataset like what was the code?