Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-04-2004, 04:53 AM   PM User | #1
richard_23
New to the CF scene

 
Join Date: Dec 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
richard_23 is an unknown quantity at this point
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).
richard_23 is offline   Reply With Quote
Old 02-04-2004, 05:25 AM   PM User | #2
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
Won't you get an array of dept_name when you retrieve the value of the select boxes from JSP?

Something like this?

<%
String[] deptNames = request.getParameterValues("dept_name");
%>
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Old 02-05-2004, 11:46 AM   PM User | #3
richard_23
New to the CF scene

 
Join Date: Dec 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
richard_23 is an unknown quantity at this point
thanks a lot
richard_23 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:31 AM.


Advertisement
Log in to turn off these ads.