How to create a button onclick() function in velo

I’m trying to get data from the Wix database and display it on the website by clicking a button.
The code:

$w("#button6").onClick( (event, $w) => {
    wixData.query("Submition")
  .find()
  .then( (results) => {
    console.log(results.items);
    $w('#text10').html = results.items[0].email;
  } );
} );

Thanks in advance!

Hi there :wave:t2: What are you trying to find in your query? What does your database look like?
For speed reasons you might want to run the query on page load and fill the results on button click, instead of having to wait for it to run once you click.

Thanks for replying! What I actually want to do is send an email that corresponds to the code that the user fills the input field with. For example, if a user writes the code “23222” and he clicked the button “Submit” it will send an email to “someone@gmailcom”. The idea of running the query on page load is a very good idea, even though I don’t think it applies in my case. I was just trying to get the email back to the page.

Could you help me with the email sending?

To find the email that goes with the discount code you can use something like the code below. I tested it in a dummy database and it works as expected.
For the first section of code, you’ll need to add a click event to your submit button. Then make sure all the element IDs are correct, and that the database name and field keys are correct in the query.

import wixData from 'wix-data';

export function submitButton_click(event) {
    let disCode = $w("#input1").value; //change to your input field ID
    disCodeQuery(disCode);
}

function disCodeQuery(disCode) {
    wixData.query("YourDatabase")//change to your database name
        .eq("discountCode", disCode) // discount code field key
        .find()
        .then((results) => {
            let email = results.items[0].email;
            if (results.items.length > 0) {
                $w("#emailText").text = email; //change to your text ID
            } else {
                console.log("none")
            }
        });
}

But sending an email is above my pay grade :stuck_out_tongue_winking_eye: I recommend searching the forum (there are a lot of similar questions), and/or opening a new thread with that question.