Hi,
Yes, to be able to write the value in the field, you should add this field to your form.
Here is an article which explain how to hide fields on Form datapage.
I'm glad that script works.
Since you are using Java Script, I would like to recommend you using developer tools for debugging Java Script code. Press F12 to call developer tools in Chrome an IE or install firebug free add-on for Firefox.
If there are any errors in your code you'll see them in console.