I'm trying to create a random number automatically for the 10 Digit ID Code that will save into my database collection along with information the customer put in the form
And it will display on the database collection row along with the information of the customer.
I research and manage to create a code that generate random number. See below:
$w.onReady( function() {
function randomNumber (len) {
var x;
var n = '';
for(var count = 0; count < len; count++) {
randomNumber = Math.floor(Math.random() * 10);
n += randomNumber.toString();
}
return n;
}
$w('#input25').value = randomNumber(10);
} );
But I'm still struggling how to insert it into my data collection. I try to connect the input element into the database but it won't display the generated number and I try the "wixData.insert " in "onClick" but it just create 3 rows in my collection and I try again the "wixData.insert " now I include all my input elements it works but I have a email notification that will trigger in "onAftersave" so I discard it. I did everything I know but still no luck.
Hi,
This codes you input in the Data.js? I Want to make a form similar to yours , and all this information goes to a collection automatic, but not understanding how to apply this .
Please help.
Thank you for your help Tal and Liran! :)
Thank you Yisrael, that close bracket solved my problem.
hahahaha! you're a savior!
Good night!
My day just starting Yisrael. :D
Yay! Just getting ready to call it a day and got the notification on your latest post. Great news! Now I don't have to use my brain any more. 🤯
Have a good night. I know I will will.
Take care,
Yisrael
Solution!
export function regdog_beforeInsert(item, code) { let hook = code; item.code = randomNumber(10); return item; } function randomNumber (len) { var n = ''; for(var count = 0; count < len; count++) { randomNumber = Math.floor(Math.random() * 10); n += randomNumber.toString(); } return n; }
Thank you Yisrael.
I hope we can continue it tomorrow!
Hey Geo,
Sorry, but it's late and I'm going off the grid soon. Until tomorrow, you can entertain yourself with the following:
About Data Hooks
How to Use Data Hooks
wix-data.Hooks
wix-dataset.onAfterSave()
wix-dataset.onBeforeSave()
These should give you the information that you need to get you code working. I'll be back tomorrow so try it out and get your questions ready.
Good night,
Yisrael
Thank you Yisrael ,
I really don't know what I'm doing. I just want to insert a random number (ID Code, 10 Digit ID Code) into my database collection along with the info customer enter into the form so it's just like a ID and Tal suggest to use beforeInsert , and I don't know how to add data hook.
Hey Geo,
Fancy running into you here. 😉
Your code has a number of syntax errors. You'll need to fix those first before moving on.
You'll need a closing bracket in regdog_beforeInsert():
export function regdog_beforeInsert(item, context) { let hookContext = context; item.someRandomNumber = randomNumber(10); return item; } // need a closing bracket
As for the rest of the code, not sure what's going on. The error on the #input25 selector name seems to indicate that you don't have a component of that name. Can you shed some light on that?
Yisrael
Hi Liran,
Good day!
Thank you for the response.
Am I doing it right?
Sorry, I'm not really good at this. Could you please lend me some of your knowledge how to do it right?
Hi Geo,
The example is in the link, you can add your code there:
export function myCollection_beforeInsert(item, context) { let hookContext = context; // see below // some change to the received item item.someRandomNumber = randomNumber(10); return item;
Hey Tal,
Hello from the other side. :)
Do you have a example how to do a beforeInsert insert code? I look the link on the beforeInsert, I'm a bit confuse, I'm just new in javascript.
Hi,
You can generate the number and save it using the beforeInsert hook . In order to save the string as a number, you can use the parseInt function.
Tal.