Conditionally show button

First off - I’m not an advanced developer at all but I have a little experience.

What I would like to do is show a button if the user has submitted to a database that I’ve created.

Can anyone point me in the right direction please?

Do a query of tthe database that gets results for data submitted bby the user.
WixData Query would bbe
.eq(“_owner”, wixUsers.currentUser.id)

Get the # of results
If 0 no data. If >0 then the uuser has submitted data

Hi,
First of all set the element to “hidden on load” in the properties panel, then you can show it using:

$w("#myElement").show();

In order to show the button, the user must submit data every time he visits the page? or only once?
If the user has to submit data every time, show the button after the insert function.
If the user can submit only once to see the button add a query to the onReady function and check if the user already submitted.

Good luck :slight_smile:

The button would show only when they submitted once. So really, if there’s any data for the user in the database, then the button would show.

I’m familiar with the .show and .hide functions and I’m currently using them now.

I’m a little shaky on running a query for my database.

Would the function be…

function checkDB( ){
wixData.query(“myCollectionName”)
.find( )
.then( (results) => {
results.forEach((result)=>{
if (result.fieldName > 0){
$w(’ # button’).show();
}
else {
$w(’ # button’).hide();
});
.catch( (err) => {
let errorMsg = err;
} );
}

Anyone have any ideas on this?

Hi,
You need to search if there is a record for the specific logged-in user, in order to do so you need to get the current user, use the wix-users API to get it.
In your query, search for the relevant record using “.eq” like this:

wixData.query("myCollection")
  .eq("user", currentUser)// filter the records by the current user
  .find()
  .then( (results) => {
if(results.items.length > 0){//run code only if the query returned record 
    //your code here
    }
  } )
  .catch( (err) => {
    let errorMsg = err;
  } );

Good luck :slight_smile:

1 Like

So far I have…

let currentUser = wixUsers.currentUser;

function checkDB( ){
wixData.query(“myCollection”)
.eq(“user”, currentUser)
.find()
.then( (results) => {
if (results.items.length > 0){
$w(‘#button’).show();
}
else {
$w(‘#button’).hide();
}
} )
. catch ( (err) => {
let errorMsg = err;
} );

I think I’m really close here but I’m not getting the desired results…I also have the button I want to show set to Hide on Load.

Is that correct? What changes do I need to make to my code?

1 Like

You need to change the name of the database from “myCollection” to the actual name of your database.

Thanks. I am aware that I need to change that and the button name. Other than that, I’m good?

1 Like

Hi,

Following up on above, I wrote below code and put the button to hidden in the properties panel. However, it looks like the code is not running. Would someone be able to help?

Many thanks!

import { currentMember } from ‘wix-members’ ;
import wixData from ‘wix-data’ ;

$w . onReady ( function () {
let currentUser = currentMember ;
function checkDB ( ){
wixData . query ( “alumni_profiles” )
. eq ( “user” , currentUser )
. find ()
. then ( ( results ) => {
if ( results . items . length > 0 ){
$w ( ‘#changeprofile’ ). show ();
}
else {
$w ( ‘#changeprofile’ ). hide ();
}
} )
. catch ( ( err ) => {
let errorMsg = err ;
} );
}
})

Hey Anne,

Thanks for also asking this as a separate post +1.
To avoid confusion for those involved, I’ll close this one so you’ll be able to get more specific feedback on your new post, and so persons don’t have to use this one as the reference.

anne’s post - https://www.wix.com/velo/forum/coding-with-velo/conditionally-shown-buttons

1 Like