# code to allow user account to fill a form just one time

**URL:** <https://forum.wixstudio.com/t/code-to-allow-user-account-to-fill-a-form-just-one-time/12600>\
**Category:** Ask the community\
**Tags:** question, code, wix-stores\
**Created:** [December 8, 2018, 9:36am UTC](https://forum.wixstudio.com/t/code-to-allow-user-account-to-fill-a-form-just-one-time/12600 "2018-12-08T09:36:30Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![kabaolory](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/kabaolory/32/6561_2.png) [@kabaolory](https://forum.wixstudio.com/u/kabaolory)\
**Post date:** [December 8, 2018, 9:36am UTC](https://forum.wixstudio.com/t/code-to-allow-user-account-to-fill-a-form-just-one-time/12600/1 "2018-12-08T09:36:30Z")

</div>

Hello, i’m newbee. I have never used wix code. I don’t know how to store the user ID when logged. I want my users to be able to fill a form only one time. After submit the form is replaced by a sentence even if the user disconnect and reconnect later.

---

<div class="post-metadata">

**Author:** ![mikemoynihan99](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/mikemoynihan99/32/5930_2.png) [@mikemoynihan99](https://forum.wixstudio.com/u/mikemoynihan99)\
**Post date:** [December 8, 2018, 1:29pm UTC](https://forum.wixstudio.com/t/code-to-allow-user-account-to-fill-a-form-just-one-time/12600/2 "2018-12-08T13:29:23Z")

</div>

When they fill the form in for the first time pass their email to the collection/database along with the rest of the form info.

When a user tries to fill in the form search the collection/database for their email, if there email is found the database do not allow them to submit the form.

To get the userID and email for a logged in member use the code below:

**import** wixUsers **from** ‘wix-users’;

$w.onReady( **function** () {

**if** (wixUsers.currentUser.loggedIn) {

**let** user = wixUsers.currentUser;

console.log(user);

```
    user.getEmail() 

        .then((email) => { 

```

**let** userEmail = email;

console.log(userEmail);

})  
}  
})

---

<div class="post-metadata">

**Author:** ![kabaolory](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/kabaolory/32/6561_2.png) [@kabaolory](https://forum.wixstudio.com/u/kabaolory)\
**Post date:** [December 8, 2018, 2:09pm UTC](https://forum.wixstudio.com/t/code-to-allow-user-account-to-fill-a-form-just-one-time/12600/3 "2018-12-08T14:09:09Z")

</div>

Hello thanks for your answear.

I have actually had an idea but for the moment i’m not really successfull. I putted my form in a box1 and created a box2 with a text inside.

My idea is to diplay box1 on load when a cell in my database is empty but if there is a value in the cell hide box1. At the same time show box2 if the cell is not empty.

and checking the value of the box on form submit aswell as on page load

So far here is the code i came up with (there is a colapse box inside box1)… obviously it doesn’t work i’m a newbee

$w.onReady( **function** () {  
$w(“#dataset2”).onReady(()=\>{  
**if** ($w(“#dataset2”).getCurrentItem().participate === “yes”){  
$w(“#subscription”).hide();  
}  
});  
});

$w.onReady( **function** () {  
$w(“#dataset2”).onReady(()=\>{  
**if** ($w(“#dataset2”).getCurrentItem().participate){  
$w(“#racer”).show();  
}  
});  
});

**function** toggleFold(index) {  
**let** $fold = $w(‘#fold’ + index);  
**let** $arrowDown = $w(‘#arrowDown’ + index);  
**let** $arrowRight = $w(‘#arrowRight’ + index);  
// toggle the fold at the index  
**if** ($fold.collapsed) {  
$fold.expand();  
$arrowDown.show();  
$arrowRight.hide();  
}  
**else** {  
$fold.collapse();  
$arrowDown.hide();  
$arrowRight.show();  
}  
// collapse the other folds  
[1]  
.filter(idx =\> idx !== index)  
.forEach(idx =\> {  
$w(‘#fold’ + idx).collapse();  
$w(‘#arrowDown’ + idx).hide();  
$w(‘#arrowRight’ + idx).show();  
})  
}

export function headerBox1\_click\_1(event) {  
toggleFold(1);  
}

---

<div class="post-metadata">

**Author:** ![mikemoynihan99](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/mikemoynihan99/32/5930_2.png) [@mikemoynihan99](https://forum.wixstudio.com/u/mikemoynihan99)\
**Post date:** [December 8, 2018, 2:10pm UTC](https://forum.wixstudio.com/t/code-to-allow-user-account-to-fill-a-form-just-one-time/12600/4 "2018-12-08T14:10:57Z")

</div>

@kabaolory  
and then?

---

<div class="post-metadata">

**Author:** ![kabaolory](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/kabaolory/32/6561_2.png) [@kabaolory](https://forum.wixstudio.com/u/kabaolory)\
**Post date:** [December 8, 2018, 2:20pm UTC](https://forum.wixstudio.com/t/code-to-allow-user-account-to-fill-a-form-just-one-time/12600/5 "2018-12-08T14:20:01Z")

</div>

@mikemoynihan99 it doesn’t work i think my first mistake is i don’t know how to call the value of the cell in a index to compare it and than make an action based on the result of the copareason. secondly i dont know how to have the same action based on two different events. To be honest i feel like i’m in way over my head…

---

<div class="post-metadata">

**Author:** ![mikemoynihan99](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/mikemoynihan99/32/5930_2.png) [@mikemoynihan99](https://forum.wixstudio.com/u/mikemoynihan99)\
**Post date:** [December 8, 2018, 2:28pm UTC](https://forum.wixstudio.com/t/code-to-allow-user-account-to-fill-a-form-just-one-time/12600/6 "2018-12-08T14:28:03Z")

</div>

@kabaolory  
ok baby steps…

Are you allowing non-members to fill in the form ?

Have you successfully registered a member ?

Have you successfully submitted their form data to a database ?

Have you successfully submitted the users email to data to the database ?

---

<div class="post-metadata">

**Author:** ![kabaolory](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/kabaolory/32/6561_2.png) [@kabaolory](https://forum.wixstudio.com/u/kabaolory)\
**Post date:** [December 8, 2018, 2:55pm UTC](https://forum.wixstudio.com/t/code-to-allow-user-account-to-fill-a-form-just-one-time/12600/7 "2018-12-08T14:55:29Z")

</div>

@mikemoynihan99

Are you allowing non-members to fill in the form ?  
no the form can be use only by members

Have you successfully registered a member ?  
yes a member is successfully registered

Have you successfully submitted their form data to a database ?  
i didn’t understood the question

Have you successfully submitted the users email to data to the database ?  
he actuall form succesfully send the informations in the database except for the email.  
Actually the email appear in developper consol in preview mode

 ![](https://us1.discourse-cdn.com/wix/original/3X/4/1/4167f027c8c513d58ac96b26f45a091b3f244726.jpeg)

---

<div class="post-metadata">

**Author:** ![mikemoynihan99](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/mikemoynihan99/32/5930_2.png) [@mikemoynihan99](https://forum.wixstudio.com/u/mikemoynihan99)\
**Post date:** [December 8, 2018, 3:08pm UTC](https://forum.wixstudio.com/t/code-to-allow-user-account-to-fill-a-form-just-one-time/12600/8 "2018-12-08T15:08:17Z")

</div>

@kabaolory

ok i can see from your code that you are not passing the form data to the database using wix code so you must be just linking them on the page to the relevant fields in the database. Easiest way for you to get the email address into the form data is just to put another input on the page for their email and automatically set that input value to their email then pass it to the database along with the rest of the form info. Use the below code, once you have successfully passed the member email address and form info to the database we can move onto the next step…

**import** wixUsers **from** ‘wix-users’;

$w.onReady( **function** () {

**if** (wixUsers.currentUser.loggedIn) {

**let** user = wixUsers.currentUser;

console.log(user);

```
    user.getEmail() 

        .then((email) => { 

```

**let** userEmail = email;

console.log(userEmail);

//here we automatically set the email input to the logged in member’s email address

$w(“#emailInput”).value = userEmail

})  
}  
})

---

<div class="post-metadata">

**Author:** ![kabaolory](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/kabaolory/32/6561_2.png) [@kabaolory](https://forum.wixstudio.com/u/kabaolory)\
**Post date:** [December 8, 2018, 3:19pm UTC](https://forum.wixstudio.com/t/code-to-allow-user-account-to-fill-a-form-just-one-time/12600/9 "2018-12-08T15:19:13Z")

</div>

Ok it’s working the field get automatically the email of the user

---

<div class="post-metadata">

**Author:** ![mikemoynihan99](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/mikemoynihan99/32/5930_2.png) [@mikemoynihan99](https://forum.wixstudio.com/u/mikemoynihan99)\
**Post date:** [December 8, 2018, 4:08pm UTC](https://forum.wixstudio.com/t/code-to-allow-user-account-to-fill-a-form-just-one-time/12600/10 "2018-12-08T16:08:02Z")

</div>

ok now that you have the email address in the database next step we query the database to see if we can find the logged in members email, if we find there email in the database it means they have already submitted the form previously…

**import** wixUsers **from** ‘wix-users’;  
**import** wixData **from** ‘wix-data’;

$w.onReady( **function** () {

**if** (wixUsers.currentUser.loggedIn) {

**let** user = wixUsers.currentUser;

console.log(user);

```
    user.getEmail() 

        .then((email) => { 

```

**let** userEmail = email;

console.log(userEmail);

//here we automatically set the email input to the logged in member’s email address

$w(“#emailInput”).value = userEmail

//query the database to try find their email

wixData.query(“formDatabase”) //change this to the name of your database

```
    .eq("email", userEmail) //"email is the name of the field in your database, change as necessary" 

    .limit(1) 

    .find() 

    .then((results) => { 

```

**let** allResults = results.totalCount;

console.log(allResults);//this should log 0 if they have never submitted a form before

**if** (allResults \> 0) {

console.log(“member submitted form previously”);  
}  
})  
})  
}  
})

---

<div class="post-metadata">

**Author:** ![kabaolory](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/kabaolory/32/6561_2.png) [@kabaolory](https://forum.wixstudio.com/u/kabaolory)\
**Post date:** [December 8, 2018, 4:23pm UTC](https://forum.wixstudio.com/t/code-to-allow-user-account-to-fill-a-form-just-one-time/12600/11 "2018-12-08T16:23:36Z")

</div>

@mikemoynihan99

console.log(“member submitted form previously”);  
does this line means that the page will show this value “member submitted form previously” instead of the form ?

---

<div class="post-metadata">

**Author:** ![mikemoynihan99](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/mikemoynihan99/32/5930_2.png) [@mikemoynihan99](https://forum.wixstudio.com/u/mikemoynihan99)\
**Post date:** [December 8, 2018, 4:27pm UTC](https://forum.wixstudio.com/t/code-to-allow-user-account-to-fill-a-form-just-one-time/12600/12 "2018-12-08T16:27:44Z")

</div>

@kabaolory  
No were not at that stage yet, we are only logging a message in the developer console, read what is says in the developer console when you run the code

---

<div class="post-metadata">

**Author:** ![kabaolory](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/kabaolory/32/6561_2.png) [@kabaolory](https://forum.wixstudio.com/u/kabaolory)\
**Post date:** [December 8, 2018, 4:39pm UTC](https://forum.wixstudio.com/t/code-to-allow-user-account-to-fill-a-form-just-one-time/12600/13 "2018-12-08T16:39:05Z")

</div>

@mikemoynihan99 yes it does show values

 ![](https://us1.discourse-cdn.com/wix/original/3X/2/a/2a411df772dd560ee24cccabc62015f365e688f6.jpeg)

---

<div class="post-metadata">

**Author:** ![mikemoynihan99](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/mikemoynihan99/32/5930_2.png) [@mikemoynihan99](https://forum.wixstudio.com/u/mikemoynihan99)\
**Post date:** [December 8, 2018, 4:50pm UTC](https://forum.wixstudio.com/t/code-to-allow-user-account-to-fill-a-form-just-one-time/12600/14 "2018-12-08T16:50:23Z")

</div>

@kabaolory  
ok now we are getting there. So now that we have configured the code to tell us if a member has submitted the form previously we can do things like disable the submit button, or show a message on screen to the member informing them that they have already submitted the form. Like so…

**import** wixUsers **from** ‘wix-users’;  
**import** wixData **from** ‘wix-data’;

$w.onReady( **function** () {

**if** (wixUsers.currentUser.loggedIn) {

**let** user = wixUsers.currentUser;

console.log(user);

```
    user.getEmail() 

        .then((email) => { 

```

**let** userEmail = email;

console.log(userEmail);

//here we automatically set the email input to the logged in member’s email address

$w(“#emailInput”).value = userEmail

wixData.query(“formDatabase”) //change this to the name of your database

```
    .eq("email", userEmail) //"email is the name of the field in your database, change as necessary" 

    .limit(1) 

    .find() 

    .then((results) => { 

```

**let** allResults = results.totalCount;

console.log(allResults);//this should log 0 if they have never submitted a form before

**if** (allResults \> 0) {

console.log(“member submitted form previously”);

$w(“#submitButton1”).disable(); //change the id of the button as necessary

$w(‘#errorText1’).show(‘FadeIn’); //change the id of error text as necessary, note you should set text to hidden on load in properties menu for the text

}  
})  
})  
}  
})

---

<div class="post-metadata">

**Author:** ![kabaolory](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/kabaolory/32/6561_2.png) [@kabaolory](https://forum.wixstudio.com/u/kabaolory)\
**Post date:** [December 8, 2018, 5:05pm UTC](https://forum.wixstudio.com/t/code-to-allow-user-account-to-fill-a-form-just-one-time/12600/15 "2018-12-08T17:05:31Z")

</div>

@mikemoynihan99

YES it’s working, question : does #errortext1 automatiquelly happear on submit button ?

---

<div class="post-metadata">

**Author:** ![mikemoynihan99](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/mikemoynihan99/32/5930_2.png) [@mikemoynihan99](https://forum.wixstudio.com/u/mikemoynihan99)\
**Post date:** [December 8, 2018, 5:14pm UTC](https://forum.wixstudio.com/t/code-to-allow-user-account-to-fill-a-form-just-one-time/12600/16 "2018-12-08T17:14:10Z")

</div>

@kabaolory

Good stuff. Write some text on the page something like “Form Submitted Already”. Right click the text choose VIEW PROPERTIES and set it as HIDDEN ON LOAD and set ID to " [errortext1](https://www.wix.com/code/home/forum/search/posts%3Fquery=.num.errortext1) "

When the page loads then text will be hidden. When the code runs it will un-hide the text if the user has submitted the form already. IF the user has not submitted the form previously the text will stay hidden.

---

<div class="post-metadata">

**Author:** ![kabaolory](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/kabaolory/32/6561_2.png) [@kabaolory](https://forum.wixstudio.com/u/kabaolory)\
**Post date:** [December 8, 2018, 5:17pm UTC](https://forum.wixstudio.com/t/code-to-allow-user-account-to-fill-a-form-just-one-time/12600/17 "2018-12-08T17:17:30Z")

</div>

@mikemoynihan99 yes this is exactly what it does.

What i’m wondering is when a new user submit the form. Does the page reload after form submit (in such case the wix code will run) or does the button stay usable ?

---

<div class="post-metadata">

**Author:** ![mikemoynihan99](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/mikemoynihan99/32/5930_2.png) [@mikemoynihan99](https://forum.wixstudio.com/u/mikemoynihan99)\
**Post date:** [December 8, 2018, 5:22pm UTC](https://forum.wixstudio.com/t/code-to-allow-user-account-to-fill-a-form-just-one-time/12600/18 "2018-12-08T17:22:05Z")

</div>

@kabaolory  
just set your submit button to link to a page after it submits same way you set it to submit the info to the database

---

<div class="post-metadata">

**Author:** ![kabaolory](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/kabaolory/32/6561_2.png) [@kabaolory](https://forum.wixstudio.com/u/kabaolory)\
**Post date:** [December 8, 2018, 5:22pm UTC](https://forum.wixstudio.com/t/code-to-allow-user-account-to-fill-a-form-just-one-time/12600/19 "2018-12-08T17:22:57Z")

</div>

@mikemoynihan99 thank you verry much !!!

---

<div class="post-metadata">

**Author:** ![mikemoynihan99](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/mikemoynihan99/32/5930_2.png) [@mikemoynihan99](https://forum.wixstudio.com/u/mikemoynihan99)\
**Post date:** [December 8, 2018, 5:25pm UTC](https://forum.wixstudio.com/t/code-to-allow-user-account-to-fill-a-form-just-one-time/12600/20 "2018-12-08T17:25:39Z")

</div>

@kabaolory

no problem

[Next page](https://forum.wixstudio.com/t/code-to-allow-user-account-to-fill-a-form-just-one-time/12600.md?page=2)
