Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Aug 2008
    Posts
    77
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Question JavaScript variable to form hidden variable's value

    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:
    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:
    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.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,010
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Code:
    <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

  • #3
    New Coder
    Join Date
    Aug 2008
    Posts
    77
    Thanks
    23
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    Code:
    <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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •