/    Sign up×
Community /Pin to ProfileBookmark

Names for dynamically added list boxes

I have a HTML form which contains a button. When the button is pressed a function is called which queries the database and displays a list box(list box populated with values from the database). This way user can add any number of list boxes.

Here is my code

client.jsp
/*************************/
<%@page contentType=”text/html”%>
<html>
<head>
<script type=”text/javascript” src=”scripts.jsp”></script>
</head>
<body>
<form action=”submitAddClient.jsp”>
<div id=”container”></div>
<input type=”button” onclick=”insSelect();” value=”Add Dept” />
<input type=”submit”>

</form>
</body>
</html>
/*********************/
Here is scripts.jsp
/*
**
*****************/

var myDataArray = new Array(
<%
con = ConnectionManager.getConnection();
stmt=con.createStatement();
rs=stmt.executeQuery(“SELECT dept_name FROM department “);

while( rs.next() )
{
data = rs.getString(1);
out.println(“”” + data + “”,”);
}
out.println(“”””);
con.close();
%>
);

function insSelect()
{
var containerElement= document.getElementById(“container”);
var newSelectElement = document.createElement(“select”);
newSelectElement.name=”dept_name”; //Problem Line
for (i=0; i < (myDataArray.length – 1); i++)
{

var newOptionElement = document.createElement(“option”);
newOptionElement.setAttribute(“value”, myDataArray[i]);
newOptionElement.innerHTML = myDataArray[i];

newSelectElement.appendChild(newOptionElement);
}

containerElement.appendChild(newSelectElement);
}

/*************************/

This all works fine.
But there is one problem.
Each select box is created with the name “dept_name” (when the button is pressed more than once and hence there are more than one list boxes). So when i submit the form I get value of only one list box(even when there are multiple list boxes).

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@PittimannFeb 04.2004 — Hi!

Why not add a number to the names?

[code=php]var selNum=0;
function insSelect() {
selNum++;
var containerElement= document.getElementById("container");
var newSelectElement = document.createElement("select");
newSelectElement.name="dept_name"+selNum;
for (i=0; i < (myDataArray.length - 1); i++) {
var newOptionElement = document.createElement("option");
newOptionElement.setAttribute("value", myDataArray[i]);
newOptionElement.innerHTML = myDataArray[i];
newSelectElement.appendChild(newOptionElement);
}
containerElement.appendChild(newSelectElement);
}
[/code]

Cheers - Pit
Copy linkTweet thisAlerts:
@andrew_123authorFeb 04.2004 — Thank You so much Pit
×

Success!

Help @andrew_123 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,
)...