So, I have multiple questions, and each question is its own page. On every page, I have created an input box so users can enter their answer to my question. There’s also a submit button that submits the info to a database that I created.
Is it possible to reference the database and find out if a user has already answered a question/clicked submit button on that page so that I can make a hidden text appear that says "This question was already answered "?
I would like the previously hidden now visible text to remain visible even if the user logs off and on again or refreshed the page.
Hi @malikahahughes , you can by getting the current item from the dataset when the page is ready, and disable or even collapse the ‘submit’ button and the text box.
Here’s how:
$w.onReady(()=>{
$w('#dataset1').onReady(async()=> {
let item = $w('#dataset1').getCurrentItem();
if (item.answer) {
$w('#submitButton').disable();
$w('#submitButton').label = 'This question was already answered';
}
})
})
This will disable the button, so users who already answered the question won’t be able to answer again (edit their answer).
Also, the next line will change the button label from ‘Answer’ or ‘Submit Answer’ to ‘This question was already answered’.
You can instead use a text element to show this message, make sure it’s collapsed on load, then expand the text element and show its message.
if (item.answer) {
$w('#submitButton').disable();
$w('#answered').text = 'This question was already answered';
await $w('#answered').expand();
}
Ahmad it’s not working. When I try it on my live site, I put the initial answer in and submit. Then when I go back to the page, the button is not disabled and the text doesn’t show.
@ahmadnasriya I tried changing the IDs, and field names to match mine but nothing is happening still. For the getCurrentItem() , is there supposed to be anything in the parenthesis?
And the if (item.answer) that line of code, what does it mean? Am I supposed to change it at all?
And what does the async and await commands do? I haven’t come across that coding before
@malikahahughes no nothing should be between the parenthesis, the getCurrentItem() does NOT accept arguments as parameters.
Of course you need to change the if ( item.answer ), it’s the heart or the core of the whole code, the " answer " in the "item. answer " represents the field in your database that holds the answers from your visitors, what it is actually doing is checking whether there’s an answer or generally any text stored in that field, so you need to change the field to the one that holds the answers in your database.
As for the async/await , it’s really a long topic, but in general, it makes sure the line of code has finished its task before going to the next line, and in your case, it make sure the text element is shown before the page leads, so users won’t have any second to submit new data befor the code disable the button.
Try changing the field in the conditional statement to yours and it should work.
I’m trying to use the Owner field key because there’s going to be lots of answers stored in the database: the answers of all of users and all of the questions will be there, if that makes sense…
So, would I take the Owner field key (_owner), and do if(item._owner) or would it be if(_owner.answer) or something completely different?
Maybe I can’t use the Owner field key, if so, using the answer field key, how do I make sure that it applies to the right user and not someone else? In my mind I feel like I should mention in the code the owner/user so that the code knows exactly where to check.
Includes example numbers and all of this info is found in one database
(Example:
John’s OwnerFK is 123456789 and his answer for question 1 is blue
J0hn’s OwnerFK is 987654321 and his answer for question 5 is green)
How do I make sure that if he opens the same question again, John’s answer of blue is checked and not compared to J0hn’s answer of green?
Hi @malikahahughes , I can see the problem now, looks like you’re not filtering the dataset based on the current user, please set a filter on the dataset like this.
First import Wix Users and Wix Data since we’ll need it, put it on top of everything in your page code (not the site code)
import wixUsers from 'wix-users';
import wixData from 'wix-data';
let user = wixUsers.currentUser;
Then add the filter inside the page’s onReady() function:
here’s what I have with all of my Ids, but for some reason it still won’t notify me if it’s been answered whether I try it in preview or live. I always make sure to have already submitted the answer so that way I could practice with my Id, but nothing’s happening.
@malikahahughes Maybe the structure is built incorrectly, since I can’t access your site, please ask one of the Corvid Masters to check your site by giving them the editor link (only them have access to your editor).
The developer console is telling me that I can’t set a filter on a “write only dataset”, but I don’t know if I should change that because it shows their answer again. Or maybe that wouldn’t matter because they already submitted…?
@ahmadnasriya I’m sorry, this is frustrating, I tried the code exactly as you had it except with my IDs in, and the text is showing and the submit button is disabled right when the page loads, even when there wasn’t a previous submission.
then I spent a few hours messing around with some of the elements, but still, nothing works
Thanks for note @shantanukumar847 , I already know that, but I assumed that he set a “Member Only” permission on the page.
@malikahahughes , the code should work in theory, you might be missing something, but I can’t tell what it is, maybe shan can spot the issue, he’s experienced much more than me.