/    Sign up×
Community /Pin to ProfileBookmark

Setting a Hidden Item

<form action=”indexedParameters.jsp” method=”POST”>
<input type=”hidden” name=”indexed” value=”1″>
<input type=”hidden” name=”indexed” value=”2″>
<input type=”hidden” name=”indexed” value=”3″>
</form>

Hi how can I replicate the above scenario from the script.

I wanted to do something like this
for (i=1, i< noOfJobs; i++) {
fillListForm.selectedJobs.value =
fillListForm.selectItem.options[i].text + “*” +
fillListForm.selectItem.options[i].value;
}

where “selectedJobs” is a hidden item.

In the receiving page I find that selectedJobs contains only one value and that is the last value. We can understand that that is expected since the value is getting overwritten inside the loop.
I however noticed that document.fillListForm.selectedJobs.value[i] does not throw any errors altho it did not work.

Any thoughts pls

?

to post a comment
JavaScript

11 Comments(s)

Copy linkTweet thisAlerts:
@lilluOct 11.2003 — Only 1 value is passed because the name attribute of the hidden input fields is identical. You should name them differently, eg. indexed1, indexed2, ...etc, then use a for loop to go through all elements of the form and put the values into a single string eg.1+2+3 or indexed1=1...
Copy linkTweet thisAlerts:
@Khalid_AliOct 11.2003 — you need to add more hidden fields or create a string of values

string = val * val;+",";

Notice I separate each value with a comma,once you have all of the values then you cna use the split function to separate these values
Copy linkTweet thisAlerts:
@aakilauthorOct 11.2003 — <form action="indexedParameters.jsp" method="POST">

<input type="hidden" name="indexed" value="1">

<input type="hidden" name="indexed" value="2">

<input type="hidden" name="indexed" value="3">

</form>

Hi Khalid and Lillu

Thank you for the mail. But if you had noticed, my form item, you can see that the variable "indexed" get three values. These values are not overwritten. In the receiving jsp page if I give

String[] my Values = request.getParameterValues("indexed");

I would get all the three values as strings into a string array. I did not create a huge string by appending and later get the values by splitting, the variable "indexed" acts as an array. I wish there would be a way to do the same using script.

regards

Thahir ?
Copy linkTweet thisAlerts:
@Khalid_AliOct 11.2003 — if you have an array of hidden fields then your loop code should be like this

for (i=1, i< noOfJobs; i++) {

fillListForm.selectedJobs[i].value =

fillListForm.selectItem.options[i].text + "*" +

fillListForm.selectItem.options[i].value;

}
Copy linkTweet thisAlerts:
@aakilauthorOct 11.2003 — I fully agree with you Khalid, but I am not able to make it work. I found out from here http://www.mals-e.com/support/prscr.htm?pg=help3 that I can declare an array like this. But when I try to put values to it from the script the way you mentioned I get errors.

In other words how do I declare an array as a hidden feild. Is it not

<input type="hidden" name="selectedJobs[]">

regards

Thahir
Copy linkTweet thisAlerts:
@lilluOct 11.2003 — Sorry, I made a mistake. It was not the name attribute that was the problem. Here's a working example:

<html>

<head>

<title>Hidden fields loop</title>

<script type="text/javascript" language="javascript">

function check_Fields(f) {

var element_value, str='';

for (i = 0; i < 3; i++) {

element_value = f.elements[i].value;
str += 'Element['+i+'] value: ' + element_value + 'n';

}

alert(str);

return false; //cancel submission

}

</script>

</head>

<body>

<form name="form1" method="post" action="" onSubmit="return check_Fields(this)">

<input type="hidden" name="indexed" value="1">


<input type="hidden" name="indexed" value="2">

<input type="hidden" name="indexed" value="3">

<input type="submit" name="Submit" value="Submit">

</form>

</body>

</html>
Copy linkTweet thisAlerts:
@aakilauthorOct 11.2003 — Thank You Lillu

The form input hidden type I gave as an example. What I meant was, I can use the same variable name and assign multiple values to it manually now how to do the same using script. My form contains "n" number of elements, the number is only known at run time. I need to assign an array like this

<input type="hidden" name="selectedJobs[]">

and should be able to assign values to the array from a script like this

for (i=1, i< noOfJobs; i++) {

fillListForm.selectedJobs[i].value = new Option(val1, val2)

}



In the receiving jsp page I should get the values as an array, the requirements are like a shopping cart scenario, but the number of items selected are many, so it is not very clean code when you make up a huge string and later split it.



My basic needs are two

1) how to declare an array as a input feild

2) how to fill the array using scripts



thanks again



Thahir
Copy linkTweet thisAlerts:
@aakilauthorOct 11.2003 — Sorry Lillu

by

for (i=1, i< noOfJobs; i++) {

fillListForm.selectedJobs[i].value = new Option(val1, val2)

}



I meant



for (i=1, i< noOfJobs; i++) {

fillListForm.selectedJobs[i].value = newValue

}



I guess I need some sleep now ?
Copy linkTweet thisAlerts:
@Khalid_AliOct 11.2003 — [i]Originally posted by aakil [/i]

[B]<input type="hidden" name="selectedJobs[]">

[/B]
[/QUOTE]


The above seems correct syntax( for php at least) I have never used it like that in jsp..so I guess try it an let us know as well..
Copy linkTweet thisAlerts:
@aakilauthorOct 12.2003 — Hi

I am trying to pass an array of strings between pages can somebody pls explain how to set values to an array thru a javascript method.

This is what I had done so far

//this is "abc.jsp"

<head>

<script language="JavaScript">

function trys() {

//this javascript loop is throwing an error, saying

// "document.see.sels is null or not an object" I am trying it with IE6

for (i=0; i<5; i++) {

document.see.sels[i].value = "changed "+i;

}

window.see.action="bcd.jsp";

window.see.submit();

}

</script>

</head>



<body>

<form name="see" action="" method="post">

<input type="button" name="but" value="look" onclick="trys()">

<input type="hidden" name="sels[]">

</form>

</body>







//this is "bcd.jsp"

<%

String[] ans = request.getParameterValues("sels");

if (ans != null) {

for (int i=0; i<ans.length; i++) {

System.out.println("sels:"+ ans[i]);

}

}

%>
Copy linkTweet thisAlerts:
@Khalid_AliOct 12.2003 — Below is the working example,I have tested it and it displays all th evalues in the hidden fields.

<?xml version="1.0"?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<title>Untitled</title>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-5"/>

<meta name="ROBOTS" http-equiv="" content="ALL,INDEX"/>

<meta name="Author" content="Khalid Ali"/>

<%

if(request.getParameter("Submit")!=null){

java.io.PrintWriter writer = response.getWriter();

String[] data = request.getParameterValues("t1");

int len = data.length;

for(int n=0;n<len;n++){

writer.println("hidden fields array t1["+n+"] , --- value = "+data[n]+"<br/>");

}

}

%>

<script type="text/javascript">

<!--

function Process(){

var frm = document.getElementById("form1");

var obj = frm.t1;

var len = obj.length;

for(var n=0;n<len;n++){

obj[n].value = "hidden_field_"+(n+1);

}

}

//-->

</script>

</head>

<body>

<form id="form1" action="" onsubmit="">

<input type="hidden" name="t1"/><br/>

<input type="hidden" name="t1"/><br/>

<input type="hidden" name="t1"/><br/>

<input type="hidden" name="t1"/><br/>

</form>

</body>

</html>
×

Success!

Help @aakil spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...