...

View Full Version : JavaScript variable to form hidden variable's value



xeirus
11-14-2010, 12:35 PM
Hi!
I have a javascript in the head of the document which has a variable named "ref2" ... ref2 is already working as I can see its value working in another function.
I need to send this variable as the value of a hidden field in the form which is in the body of the document.

This is my JavaScript Code:


function WriteContactFormStatement1 () {
var ContactFormValue = ref2;
document.write('<input type="hidden" name="UReferrersName" value="' + ContactFormValue + '" />');
}
var WriteContactFormStatement = WriteContactFormStatement1 ();


And at the end of my form, before the submit button, I have the following code:


<!-- START -- Javascript to print the statement for UReferrersName -->
<script language="JavaScript" type="text/JavaScript">
//WriteContactFormStatement();
document.write (WriteContactFormStatement);
</script>
<!-- End -- Javascript to print the statement for UReferrersName -->


When I execute the form, it doesn't work the way it should, plus, gives me a word "undefined" next to the "Submit" button .....

Please help !...

- Xeirus.

Philip M
11-14-2010, 01:00 PM
<script type = "text/javascript">
var ref2 = "XXX"; // ref2 must be a global variable when it is defined;
function WriteContactFormStatement1 () {
ContactFormValue = ref2;
document.write('<input type="text" name="UReferrersName" value="' + ContactFormValue + '" />');
}

WriteContactFormStatement1();

</script>


<script type="text/JavaScript">

WriteContactFormStatement1();

</script>

After testing change <input type = "text" to <input type = "hidden".

We have been asked to correct our report on the wedding last week. On return from honeymoon the happy couple will be staying at The Old Manse and not as we stated at the home of the bridegroom's father. - Woking Informer

xeirus
11-14-2010, 01:12 PM
<script type = "text/javascript">
var ref2 = "XXX"; // ref2 must be a global variable when it is defined;
function WriteContactFormStatement1 () {
ContactFormValue = ref2;
document.write('<input type="text" name="UReferrersName" value="' + ContactFormValue + '" />');
}

WriteContactFormStatement1();

</script>


<script type="text/JavaScript">

WriteContactFormStatement1();

</script>

After testing change <input type = "text" to <input type = "hidden".



Thank you very much Philip M !
That worked like a charm !
Thanks a lot !

- Xeirus



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum