Update collection's item without submit button

Before user clicks “Buy now” button (which is a default PayPal button), I would like to collect user’s email address with a User Input text box.
Problem is that there is no “Submit” button on this page. How to update the record/data collection’s email address for the latest item?

I had a try to update the email address in below function. It seems this function is not been called.

export function inEmailAddr_change(event, $w) {
// Find the latest user input and update the email address.
wixData.query(“UserInputs”)
.limit(1)
.find()
.then( (results) => {
if (results.items.length > 0)
{
let item = results.items[0];
item.dbcEmail = $w(‘#input1’).text; // a user input box will be added to update email.
wixData.update(“UserInputs”, item);
}
} )
. catch ( (err) => {
let errorMsg = err;
} );
}

Below is the page of question:
https://www.goodchinesename.com/copy-of-userinputs-dbgivenname
Or you may visit the homepage www.goodchinesename.com , then input any random name. Click “submit” button. It will leads you to the second page in question.

Yours,
Francis

Francis,

Are you using a dataset on this page? If so, the following code (with names changed as needed), placed in the blur event of the input or perhaps in the mousein event of the PayPal button, would write the data just for that field.

$w(‘#dataset1’).setFieldValue(“email”,$w(‘#input1’).value);

$w(‘#dataset1’).save()
.then((item) => {
console.log(“data was saved.”);
})
. catch ((err) => {
let errMsg = err;
});

Hi Anthony,
Thank you for the quick help.
The issue has been resolved by recreating a onChange function with the user input text box. Then the newly created onChange function is called.
My original code above works fine with one modification with your hint:
Change from: item.dbcEmail = $w(’ #input1 ‘).text;
to: item.dbcEmail = $w(’#input1’).value;

Below is the complete code:
export function input1_change(event) {
// Find the latest user input and update the email address.
wixData.query(“UserInputs”)
.limit(1)
.find()
.then( (results) => {
if (results.items.length > 0)
{
let item = results.items[0];
item.dbcEmail = $w(‘#input1’).value; // updated email address
wixData.update(“UserInputs”, item);
}
} )
. catch ( (err) => {
let errorMsg = err;
} );
}

Yours,
Francis

Hey Francis,

Just wanted to say your above code just saved me when I’d nearly given up.

I wanted to submit to a collection when the user clicks a selection tag (without then having to click a submit button afterwards) … this does the job.

THANK YOU!!!