# Help getting random # on form 

**URL:** <https://forum.wixstudio.com/t/help-getting-random-on-form/11400>\
**Category:** Ask the community\
**Tags:** question, code\
**Created:** [October 28, 2022, 1:45pm UTC](https://forum.wixstudio.com/t/help-getting-random-on-form/11400 "2022-10-28T13:45:17Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![jlangman](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/jlangman/32/24820_2.png) [@jlangman](https://forum.wixstudio.com/u/jlangman)\
**Post date:** [October 28, 2022, 1:45pm UTC](https://forum.wixstudio.com/t/help-getting-random-on-form/11400/1 "2022-10-28T13:45:17Z")

</div>

Hi all, I am new here and new to trying to pull this off. After looking around I found some code and it looked like I should be able to get this going, but it’s not working. Any ideas on what the problem could be? The $w gives a warning:  
Error loading web module backend/data.js: $w is not defined  
and I see the total tip saying: Cannot find name ‘$w’.

Any help greatly appreciated, I am also have this input hidden if that makes a difference, with the output being included in the form email.

Thanks,  
Jim

```javascript
export function contact02_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;
    }
    $w("#input6") .value = randomNumber (10) ;

```

---

<div class="post-metadata">

**Author:** ![amandam](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/amandam/32/79539_2.png) [@amandam](https://forum.wixstudio.com/u/amandam)\
**Post date:** [October 28, 2022, 2:42pm UTC](https://forum.wixstudio.com/t/help-getting-random-on-form/11400/2 "2022-10-28T14:42:41Z")

</div>

Hello,

The function with the $w in it should be in the page code where that element exists.

The hook can stay in data.js as it belongs there.

---

<div class="post-metadata">

**Author:** ![jlangman](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/jlangman/32/24820_2.png) [@jlangman](https://forum.wixstudio.com/u/jlangman)\
**Post date:** [October 28, 2022, 2:47pm UTC](https://forum.wixstudio.com/t/help-getting-random-on-form/11400/3 "2022-10-28T14:47:45Z")

</div>

Hi Amanda, thanks for responding. So the " $w ( “#input6” ). value = randomNumber ( 10 );" should be on the form element on the page? Still getting into Six, can you give me a pointer on the best method to include it at the page level?  
Thanks!

---

<div class="post-metadata">

**Author:** ![amandam](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/amandam/32/79539_2.png) [@amandam](https://forum.wixstudio.com/u/amandam)\
**Post date:** [October 28, 2022, 2:56pm UTC](https://forum.wixstudio.com/t/help-getting-random-on-form/11400/4 "2022-10-28T14:56:40Z")

</div>

Hi, happy to help but I’m not sure exactly what you mean. Do you mean you are not sure what I mean by “page code”?

If that’s the case, this article may help: [Where do I put my code?](https://support.wix.com/en/article/velo-where-do-i-put-my-code)

If this still isn’t clear, let me know. I"m sure we can get y ou moving in the right direction 😀

---

<div class="post-metadata">

**Author:** ![jlangman](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/jlangman/32/24820_2.png) [@jlangman](https://forum.wixstudio.com/u/jlangman)\
**Post date:** [October 28, 2022, 3:09pm UTC](https://forum.wixstudio.com/t/help-getting-random-on-form/11400/5 "2022-10-28T15:09:43Z")

</div>

Hi Amanda,  
I think I understand where now thank you. I placed " $w ( “#input6” ). value = randomNumber ( 10 ); " on the page code tab that has the form. However I am getting an error of " ReferenceError: Can’t find variable: randomNumber" Do I need to include something else with this code or is the problem in the hook?  
Thank you!

---

<div class="post-metadata">

**Author:** ![amandam](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/amandam/32/79539_2.png) [@amandam](https://forum.wixstudio.com/u/amandam)\
**Post date:** [October 28, 2022, 3:34pm UTC](https://forum.wixstudio.com/t/help-getting-random-on-form/11400/6 "2022-10-28T15:34:40Z")

</div>

Oh, I see. To be able to use the randomNumber function in your page code and also in the data.js file, I would suggest creating the randomNumber function in a jsw file.

That way you can import and use the function in data.js and your page code without having to duplicate code.

This is an [article about web modules](https://support.wix.com/en/article/velo-web-modules-calling-backend-code-from-the-frontend) and how to import to page code, but you can import to data.js as well

Let me know if this is enough information or if anything is still unclear

---

<div class="post-metadata">

**Author:** ![jlangman](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/jlangman/32/24820_2.png) [@jlangman](https://forum.wixstudio.com/u/jlangman)\
**Post date:** [October 28, 2022, 4:04pm UTC](https://forum.wixstudio.com/t/help-getting-random-on-form/11400/7 "2022-10-28T16:04:47Z")

</div>

Hi Amanda, thanks again. I think I have ran into my limits. I’m fairly lost now. I as hoping I was close to achieving this, but seeing as I am not a full fledged dev I am thinking I won’t be able to pull this off now.

---

<div class="post-metadata">

**Author:** ![jlangman](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/jlangman/32/24820_2.png) [@jlangman](https://forum.wixstudio.com/u/jlangman)\
**Post date:** [October 28, 2022, 4:37pm UTC](https://forum.wixstudio.com/t/help-getting-random-on-form/11400/8 "2022-10-28T16:37:27Z")

</div>

Im not even sure what a jsw file is… java script wix? No idea.

---

<div class="post-metadata">

**Author:** ![amandam](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/amandam/32/79539_2.png) [@amandam](https://forum.wixstudio.com/u/amandam)\
**Post date:** [October 28, 2022, 4:37pm UTC](https://forum.wixstudio.com/t/help-getting-random-on-form/11400/9 "2022-10-28T16:37:42Z")

</div>

You are close, I think you can get there…

First you want to take the random numbers function out of the data.js file and make a new .jsw file.

Put the function in there like this

```javascript
export function randomNumber (len) {
    
    let n = '';
    for(var count = 0; count < len; count++) {
       let randomNumber = Math.floor(Math.random() * 10);
        n += randomNumber.toString();
        }
    return n;
    }

```

Then in data.js you can import the function like this:

```javascript
import { randomNumber }from 'backend/yourFileName'

```

Which will make it available to use

In your page code, you will import the exact same way as above at the top of your file, then you will be able to use the function there as well

You have already written all the code, it’s just a matter of putting it in the correct locations now! You are close for sure. Let me know if this is helpful.

If you are looking to hire a dev to either mentor or finish up for you, come to our Discord channel and post the request in jobs and collabs

---

<div class="post-metadata">

**Author:** ![jlangman](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/jlangman/32/24820_2.png) [@jlangman](https://forum.wixstudio.com/u/jlangman)\
**Post date:** [October 28, 2022, 4:39pm UTC](https://forum.wixstudio.com/t/help-getting-random-on-form/11400/10 "2022-10-28T16:39:35Z")

</div>

Thanks Amanda! It feels like I’m close… I’ll take a close look at this. appreciate your help.

---

<div class="post-metadata">

**Author:** ![amandam](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/amandam/32/79539_2.png) [@amandam](https://forum.wixstudio.com/u/amandam)\
**Post date:** [October 28, 2022, 4:39pm UTC](https://forum.wixstudio.com/t/help-getting-random-on-form/11400/11 "2022-10-28T16:39:51Z")

</div>

It is a web module which is a backend file that allows you to export your code for use in other places like your page code, see [this article for more details](https://support.wix.com/en/article/velo-web-modules-calling-backend-code-from-the-frontend)

---

<div class="post-metadata">

**Author:** ![amandam](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/amandam/32/79539_2.png) [@amandam](https://forum.wixstudio.com/u/amandam)\
**Post date:** [October 28, 2022, 4:40pm UTC](https://forum.wixstudio.com/t/help-getting-random-on-form/11400/12 "2022-10-28T16:40:52Z")

</div>

I think you definitely are, I wouldn’t give up now! Once things click, it will be so much easier for future efforts. The beginning of learning is always the hardest

---

<div class="post-metadata">

**Author:** ![jlangman](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/jlangman/32/24820_2.png) [@jlangman](https://forum.wixstudio.com/u/jlangman)\
**Post date:** [October 28, 2022, 5:16pm UTC](https://forum.wixstudio.com/t/help-getting-random-on-form/11400/13 "2022-10-28T17:16:20Z")

</div>

Hi Amanda, I think I am really close– not working yet but I think the problem is in my code on the home page, if you don’t mind taking one more peak please.

data.js code:

```javascript
import { randomNumber }from 'backend/numGen.jsw'export function contact02_beforeInsert(item, code) {
    
    let hook = code;
        
    item.code = randomNumber(10);
    
    return item;
    }

```

numGen.jsw code:

```javascript

export function randomNumber (len) {
    
    let n = '';
    for(var count = 0; count < len; count++) {
       let randomNumber = Math.floor(Math.random() * 10);
        n += randomNumber.toString();
        }
    return n;
    }

```

Home page code:

```javascript
import { randomNumber }from 'backend/numGen.jsw';

$w('#input6') .value = randomNumber(10);

```

---

<div class="post-metadata">

**Author:** ![amandam](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/amandam/32/79539_2.png) [@amandam](https://forum.wixstudio.com/u/amandam)\
**Post date:** [October 28, 2022, 5:29pm UTC](https://forum.wixstudio.com/t/help-getting-random-on-form/11400/14 "2022-10-28T17:29:16Z")

</div>

You are very close, the next thing to learn is that when importing from the backend (jsw) to the frontend(page code) is that the backend function will always be a promise, so you need to place this code inside your onReady

```javascript
randomNumber(10).then((result) =>{
         $w('#input6').value=result
     })

```

---

<div class="post-metadata">

**Author:** ![jlangman](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/jlangman/32/24820_2.png) [@jlangman](https://forum.wixstudio.com/u/jlangman)\
**Post date:** [October 28, 2022, 6:54pm UTC](https://forum.wixstudio.com/t/help-getting-random-on-form/11400/15 "2022-10-28T18:54:01Z")

</div>

Amanda!!! Thank you it’s working now. Couldn’t have learned this without you. Really appreciate it!

---

<div class="post-metadata">

**Author:** ![jlangman](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/jlangman/32/24820_2.png) [@jlangman](https://forum.wixstudio.com/u/jlangman)\
**Post date:** [October 29, 2022, 1:25pm UTC](https://forum.wixstudio.com/t/help-getting-random-on-form/11400/16 "2022-10-29T13:25:09Z")

</div>

Hi Amanda, I have managed to hit one more snag, when I have this set up with multiple forms, it’s not always working- it works great for me but when I have others test it it doesn’t work on all 3 pages. Really weird. , I am guessing I did the data.js wrong? Maybe I need to see if theres a way to group these into a more condensed version or something?

```javascript

import { randomNumber }from 'backend/numGen.jsw'

export function enterContest063_beforeInsert(item, code) {
    
    let hook = code;
        
    item.code = randomNumber(6);
    
    return item;
    }

export function enterContest064_beforeInsert(item, code) {
    
    let hook = code;
        
    item.code = randomNumber(6);
    
    return item;
    }
    
export function enterContest06_beforeInsert(item, code) {
    
    let hook = code;
        
    item.code = randomNumber(6);
    
    return item;
    }

```

```javascript
Example as set up on one of the pages:

import { randomNumber }from 'backend/numGen.jsw';

$w.onReady(function () {

randomNumber(6).then((result) =>{
         $w('#input10').value=result
     })
    
});

```

Thanks for any more advice you can offer, I really appreciate it. I also understand if you are done with me lol.

Thanks!

Jim

---

<div class="post-metadata">

**Author:** ![jlangman](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/jlangman/32/24820_2.png) [@jlangman](https://forum.wixstudio.com/u/jlangman)\
**Post date:** [November 1, 2022, 2:09pm UTC](https://forum.wixstudio.com/t/help-getting-random-on-form/11400/17 "2022-11-01T14:09:18Z")

</div>

Bummer I have had to bail on this since I can’t get it to work consistently when having others testing it. I really wish there was a built in function for something like this.
