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

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.

1 Like

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);

})
}
})

2 Likes

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);
}

@kabaolory
and then?

@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…

@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 ?

@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

@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

})
}
})

1 Like

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

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”);
}
})
})
}
})

@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 ?

@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

@mikemoynihan99 yes it does show values

@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

}
})
})
}
})

1 Like

@mikemoynihan99

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

@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 "

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.

@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 ?

@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

1 Like

@mikemoynihan99 thank you verry much !!!

@kabaolory

no problem