473,608 Members | 1,809 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pass array as Hidden form field? Can it be done?

6 New Member
Hi,

I'm self learning javascript - so any pointers are welcomed!!

I have an issue passing a form and array from one function to another.
I tried many variations !
I can't get this to work and I can't get this issue out of my head !!!!
So I'm obviously missing something really simple and can't see it or it can't be done with my limited knowledge.

I know that I can use a cookie to store the array as string - I have this working but I would like not to use cookies if possible.

ok my bare code:

Expand|Select|Wrap|Line Numbers
  1. // sets up arrays
  2. function generateQuestions(myNumberOfQuestionsAsked)
  3. { // myNumberOfQuestionsAsked is integer value
  4.  
  5. var realAnswers =new Array("0010","1010");
  6. var answers = new Array();
  7.  
  8.        for(i=0;i<myNumberOfQuestionsAsked;i++)
  9.              {      answers[i]= realAnswers[i]; }
  10. displayQuestions(answers);
  11. }
  12.  
  13.  
  14. // creates the form in a table and uses the onclick to pass the form
  15. function displayQuestions(answers)
  16. {
  17. ..... within <BODY>
  18.     myTestWindow.document.write("<TABLE ID='testTable1'>");
  19. // create the FORM
  20. myTestWindow.document.write("<FORM ID='myForm' NAME='myForm'>");
  21.  
  22. // Create the HIDDEN element to hold the array
  23. myTestWindow.document.write("<INPUT TYPE='hidden' ID='mydata' NAME='mydata' VALUE=''>");
  24. myTestWindow.document.write("</FORM>");
  25. myTestWindow.document.write("</TABLE>");
  26.  
  27.     myTestWindow.document.write("<INPUT TYPE='button' value='Mark Your Answers' onClick='testResults(myForm)'>");
  28.     //the next section is used to refresh the script
  29.     myTestWindow.document.write("<SCRIPT LANGUAGE='JAVASCRIPT' SRC='quizScript1.js'></SCRIPT>");
  30.  
  31.     //take the array and put it in the hidden field in a string format
  32.     ansString = unescape(answers.join())
  33.     myTestWindow.document.myForm.mydata.value=ansString;
  34.  
  35. ...... closing tags<HERE>
  36. }
  37.  
  38. // displays the hidden value
  39. function testResults(myForm) 
  40. {
  41. alert ("You typed: "+myForm.mydata.value);
  42. }

Any help would be appreciated!

Thanks!
Oct 28 '09 #1
9 30098
nicnac
6 New Member
Hi,

I'm self learning javascript - so any pointers are welcomed!!

I have an issue passing a form and array from one function to another.
I tried many variations !
I can't get this to work and I can't get this issue out of my head !!!!
So I'm obviously missing something really simple and can't see it or it can't be done with my limited knowledge.

I know that I can use a cookie to store the array as string - I have this working but I would like not to use cookies if possible.

ok my bare code:

Expand|Select|Wrap|Line Numbers
  1. // sets up arrays
  2. function generateQuestions(myNumberOfQuestionsAsked)
  3. { // myNumberOfQuestionsAsked is integer value
  4.  
  5. var realAnswers =new Array("0010","1010");
  6. var answers = new Array();
  7.  
  8. for(i=0;i<myNumberOfQuestionsAsked;i++)
  9. { answers[i]= realAnswers[i]; }
  10. displayQuestions(answers);
  11. }
  12.  
  13.  
  14. // creates the form in a table and uses the onclick to pass the form
  15. function displayQuestions(answers)
  16. {
  17. ..... within <BODY>
  18. myTestWindow.document.write("<TABLE ID='testTable1'>");
  19. // create the FORM
  20. myTestWindow.document.write("<FORM ID='myForm' NAME='myForm'>");
  21.  
  22. // Create the HIDDEN element to hold the array
  23. myTestWindow.document.write("<INPUT TYPE='hidden' ID='mydata' NAME='mydata' VALUE=''>");
  24. myTestWindow.document.write("</FORM>");
  25. myTestWindow.document.write("</TABLE>");
  26.  
  27. myTestWindow.document.write("<INPUT TYPE='button' value='Mark Your Answers' onClick='testResults(myForm)'>");
  28. //the next section is used to refresh the script
  29. myTestWindow.document.write("<SCRIPT LANGUAGE='JAVASCRIPT' SRC='quizScript1.js'></SCRIPT>");
  30.  
  31. //take the array and put it in the hidden field in a string format
  32. ansString = unescape(answers.join())
  33. myTestWindow.document.myForm.mydata.value=ansStrin g;
  34.  
  35. ...... closing tags<HERE>
  36. }
  37.  
  38. // displays the hidden value
  39. function testResults(myForm) 
  40. {
  41. alert ("You typed: "+myForm.mydata.value);
  42. }

Any help would be appreciated!

Thanks!
Oct 29 '09 #2
nicnac
6 New Member
Sorry! As you can tell I'm new so posted this in the WRONG place!
It has now been moved to the javascript questions area! Although if you know the answer please feel free to comment!

Thanks
Oct 29 '09 #3
RamananKalirajan
608 Contributor
What about this piece of code? Did you got any error while trying this code? You can use the hidden variable to set the array values.

Thanks and Regards
Ramanan Kalirajan
Oct 30 '09 #4
nicnac
6 New Member
No I never got any errors just no data displayed?
I have checked the contents of the array and string as it's passed through the function and it's stored ok. (output in alert's)
It's just when the data is passed in the form I can't see it?

I'm thinking it must be my syntax ? Any suggestions?

Thanks,
Nichola
Oct 30 '09 #5
Dormilich
8,658 Recognized Expert Moderator Expert
do you have a test page available?
Oct 30 '09 #6
RamananKalirajan
608 Contributor
Hi,
I got the solution for your question. You will get the array values but not as a array object instead you will get as a String object. The input type hidden variables can hold only string values. When you assign a Array object to the Hidden Variable its implicitly converted to String Value.

This is the following code I tried myself
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2. <HTML>
  3. <HEAD>
  4. <TITLE> New Document </TITLE>
  5. <META NAME="Generator" CONTENT="EditPlus">
  6. <META NAME="Author" CONTENT="">
  7. <META NAME="Keywords" CONTENT="">
  8. <META NAME="Description" CONTENT="">
  9. <script type="text/javascript">
  10.     function loadArray(){
  11.         var realAnswers =new Array("0010","1010");
  12.         var answers = new Array();
  13.         for(var i=0;i<realAnswers.length;i++)
  14.         {
  15.             answers[i] = realAnswers[i];
  16.             alert(answers[i]);
  17.         }
  18.         document.getElementById('hidArray').value = answers;
  19.         alert("loaded Successfully "+document.getElementById('hidArray').value);
  20.     }
  21.     function displayArray()
  22.     {
  23.         var myhidObj = new Array (document.getElementById('hidArray').value);
  24.         alert("myhidObj = "+myhidObj);
  25.         alert("length = "+myhidObj.length);
  26.         for(var i=0;i<myhidObj.length;i++)
  27.             alert(myhidObj[i]);
  28.     }
  29. </script>
  30. </HEAD>
  31. <BODY>
  32. <form id="myForm" name="myForm">
  33.     <input type="hidden" name="hidArray" id="hidArray" value=""/>
  34. </form>
  35. <input type="button" value="loadArray" onclick="loadArray();" />
  36. <input type="button" value="displayArray" onclick="displayArray();" />
  37. </BODY>
  38. </HTML>
Thanks and Regards
Ramanan Kalirajan
Oct 30 '09 #7
nicnac
6 New Member
Oh that works a treat! Magic!!

However I want to run the javascript in a .js file. When I do I'm getting issues with the document values. When in displayQuestion s(), I create my form and new window. I can access all elements, however when I'm trying to access document.getEle mentById('hidAr ray').value from another function (displayArray() I'm getting no value?
I think the document is local to that function? How would I pass the document or would I be better passing the form that way I can access other elements too?

I have tried passing the form and accessing the element by form.elements['hidArray'] and again I see no output?
Must be my syntax - I'm sure you can pass the form this way!

My code is below:

Expand|Select|Wrap|Line Numbers
  1. function displayQuestions(selectedQuestions)
  2. {
  3.     //close the questions window
  4.     alert("The Questions will open in a new Window - Click OK to proceed ");
  5.     var length = selectedQuestions.length;
  6.     myTestWindow = window.open('','myTestWindow','scrollbars=yes,width=800,height=650,left=20,top=10');
  7.     myTestWindow.focus();
  8. //create the page dynamically
  9.     myTestWindow.document.write("<HTML><HEAD><TITLE>The Test Page</TITLE></HEAD>");
  10.     myTestWindow.document.write("<BODY bgcolor='#003366' text='#FFFFFF' >");
  11.  
  12.     myTestWindow.document.write("<TABLE ID='testTable1' border=1 bordercolor='white' CELLPADING='' CELLSPACING='0' background= '' width='90%' height='90%'ALIGN='CENTER' bgcolor='#003366'>");
  13.     myTestWindow.document.write("<TR width='100%' height='20%'>");
  14.     myTestWindow.document.write("<TD colspan=10 align=center><font color='#FFFFFF' face='Verdana, Arial, Helvetica, sans-serif'");
  15.  
  16.     myTestWindow.document.write("</TD>");
  17.     myTestWindow.document.write("</TR>");
  18.     myTestWindow.document.write("<form id='myForm' name='myForm'>");
  19.     myTestWindow.document.write("<input type='hidden' name='hidArray' id='hidArray' value=''/> ");    
  20.     /*using a for loop to dynamically generate rows and columns equal to the number of questions */
  21.         for (i=0;i<length;i++)
  22.         {
  23.          myTestWindow.document.write("<TR>");
  24.          myTestWindow.document.write("<TD valign=top id ='questionsColumn"+i+"' name= 'questionsColumn"+i+"' colspan=4>");
  25.          myTestWindow.document.write("<P><font color='#FFFFFF' face='Verdana, Arial, Helvetica, sans-serif'> Convert this to Binary</TD>");
  26.  
  27.          myTestWindow.document.write("<TD valign=top align = center colspan=2>");
  28.          myTestWindow.document.write("<INPUT TYPE='text' ID='text"+i+"' NAME='text"+i+"'>");
  29.          myTestWindow.document.write("</TD>");
  30.          myTestWindow.document.write("</TR>");
  31.         }//for
  32.  
  33.     myTestWindow.document.write("</form>");
  34.     myTestWindow.document.write("</TABLE>");
  35.  
  36.     ans= getAnswers(length);
  37.     myTestWindow.document.getElementById('hidArray').value = ans; 
  38.  
  39.     alert("loaded Successfully "+myTestWindow.document.getElementById('hidArray').value);
  40.     myTestWindow.document.write("<input type='button' value='displayArray' onclick='displayArray();' />");
  41.     myTestWindow.document.write("<SCRIPT LANGUAGE='JAVASCRIPT' SRC='quizScript1.js'></SCRIPT>");
  42. }
  43.  
  44.  
  45.  
  46. function displayArray() 
  47.     { 
  48.         alert("HERE");
  49.         var myhidObj = new Array (myTestWindow.document.getElementById('hidArray').value); 
  50.         alert("myhidObj = "+myhidObj); 
  51.         alert("length = "+myhidObj.length); 
  52.         for(var i=0;i<myhidObj.length;i++) 
  53.             alert("FINISHED"+myhidObj[i]); 
  54.     }
  55.  
  56. function getAnswers(length)
  57.     {
  58.         alert("getAns1 the length is"+length);
  59.         var realAnswers =new Array("0010","1010","0111","0011","0001","0100","0101");
  60.         var answers = new Array();
  61.  
  62.         for(i=0;i<length;i++)
  63.      {
  64.         answers[i]= realAnswers[i];
  65.      }
  66.          return answers;    
  67.   }
Oct 30 '09 #8
acoder
16,027 Recognized Expert Moderator MVP
displayArray() from the popup window expects that it is defined within the window. Since it's actually in the parent window, change it to window.opener.d isplayArray().

PS. please use code tags when posting code.
Nov 1 '09 #9
nicnac
6 New Member
Whoo hoo!!

Fantastic!
There was no way I was even thinking like that!

Thank you very much!
I just modified
document.getEle mentById('hidAr ray').value
to
window.opener.m yTestWindow.doc ument.getElemen tById('hidArray ').value
and it works perfect!

Much appreciated!

I will add code tags for any other posts - thanks for the pointer.

N
Nov 2 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

0
1498
by: Bharat Suneja | last post by:
Hi All, I'm running into issues with passing the distinguishedName of a user as a hidden form field in a form from one page to the result page. Page 1: User enters user name in a form field Page 2: Queries AD, gets distinguishedName, binds to the user. Need to add more info to the user if attributes not present. Relevant portion of FORM:
2
3052
by: Jaygo | last post by:
I want to get the values from various page links to a hidden field in a single online form. So if visitor A links to the form from apples.htm and visitor B links to the form from oranges.htm the value in the db table field would be Apples or Oranges. Any pointers appreciated. TIA John
4
3891
by: GavMc | last post by:
Hello I am new to internet programming and wonder if anyone can help me with this.... I am trying to pass a hidden field value on a form into another field on the form so that it can then be inserted in the database, to enable me then to reference that number at a later date. (The hidden value (1 for example) would then automatically get passed to the other input field.)
14
3473
by: Paul | last post by:
I want to set the page title and/or a form hidden field programatically through ASP.Net. I do not want to use something like... <% sTitle ="My Title" %> <html><title><%=sTitle%></title>..... I want to completely seperate the code from presentation. I would like to do the same thing for a value of a hidden form field but
12
7433
by: Alan Silver | last post by:
Hello, I have a page that gets passed an ID in the query string. It then uses this ID to pull info out of a database and populate controls on the page. When the page is posted back, the query string is not going to be there any more, so I need some way of storing the ID. What's the best way of doing this? The obvious thought is a hidden form field, but there doesn't seem to be a web control for this. Do I just use an ordinary HTML...
1
2600
by: xenophon | last post by:
I add a Hidden form field to my asp.net page in the codebehind, the Value peoperty is set to 0 and EnableViewState is set to true. Then I add a LiteralControl with JavaScript that says to set the value to 1. If I set view the value of th ehidden form field in the debugger during the course of a PostBack, the value is still set to 0. Are there any samples of setting a vlaue with JavaScript and reading the new value in ASP.NET? Thanks.
2
3269
by: xenophon | last post by:
I added a Hidden Form Field to a form in the code behind. The value is being set in JavaScript client-side, but it is not persisting to the server in the PostBack. I know the value is being set properly because it displays in the document.write method. Create a simple page and paste the below in the code-behind (ASP.NET 1.1-SP1) using System;
2
3394
by: windsorben | last post by:
How would I pass the student's final score to a hidden form field? <script language="javascript" type="text/javascript"> <!-- var score = 0;
3
1459
by: Noah | last post by:
Hello. I have a form like so: <FORM NAME="form1" METHOD = "post"> <SELECT NAME="select"> <OPTION VALUE = "1">Option 1</OPTION> <OPTION VALUE = "2">Option 2</OPTION>
0
8069
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8488
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8160
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6826
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6017
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3972
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4036
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2479
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1611
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.