Multiple data submissions when submit button is pressed

Hello everyone! I’ve run into a strange problem that I’m not sure how to solve. Apologies if I’ve made any obvious mistakes, but I’m quite new to Wix Code!

I’ve created a form on one of my pages, with a submit button at the bottom. On clicking this button, I’d like the user ID to be recorded along with all the other data, by using the OnClick event for the submit button (button_1). The value of input1 is recorded correctly, but it seems the userID field keeps getting recorded during the very short time that the user is clicking on the button:


As you can see, multiple entries of the same user ID are created at 00:25, whereas I would ideally only want one. This is my code:


import wixUsers from 'wix-users';
import wixData from 'wix-data';
$w.onReady(function () {
 //TODO: write your page related code here...
});

export function button1_click(event) {
 //Add your code for this event here:
 let userId = wixUsers.currentUser.id; 
let NewOrder = {
 "Title": $w("#input1").value,
    };
    wixData.insert("Orders", NewOrder)
    ("Orders").setFieldValue('userid',userId);
}

Is #button1 also “connected to” Submit?


If you have Submit, then the Submit dataset action performs a dataset save and your onClick() event handler also saves. So, you have two saves.

@yisrael-wix hi, I am having the same issue… My button ‘add‘ — inserts data to data table. My problem is, whenever I click ‘add’ (like double clicking) the data inserts twice in my data table. How can I prevent it?

Hard to tell. The reason @yisrael-wix mentioned could be one source. Another known source of this problem is if you register the onClick() event handler in code, and that code gets repeated with every new submission of data. But that one should increment with every submission, so click, save once, click again, twice, click again, thrice, etc.
Show us the code, we can help you better.