Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

repopulating select boxes - JSP 1

Status
Not open for further replies.

jondubya

Technical User
Apr 16, 2005
13
GB
I'm trying to save and retrieve status for a submitted form on a JSP. Retrieving textbox answers is relatively simple but for selection boxes I'm doing this:

<%String countryDB = rs.getString("countryDB"); %> //get value last submitted

<td valign="TOP" height="21" colspan="3"> //now try and re-select it.
<select name="country" >
<option value="" >Pull down to select
<option value="Afghanistan" <%if (country.equals("Afghanistan")) {%> selected <%} %>>Afghanistan</option>
<option value="Albania" <%if (country.equals("Albania")) {%> selected <%} %>>Albania</option>
<option value="Algeria"<%if (country.equals("Algeria")) {%> selected <%} %>>Algeria</option>
...etc...etc

</td>

I'm trying to compare the value I have in the database with the value for each of the select values.

Is there an easier way to write this without having to add and change the "<%if (country.equals("selectValue")) {%> selected <%} %>" for each of the hundred countries .
You could probably do it using javaScript with the onLoad function to cycle through the values of the form, but I'm not sure what the best way to go about this is.
 
Oops, should have been

<%if (countryDB.equals("Afghanistan")) {%> selected <%} %>

as opposed to

<%if (country.equals("Afghanistan")) {%> selected <%} %>
 
Hi,

I think the JavaScript is the only solution if you have hard coded the country names in the JSP page. But I would strongly recommend to store those values of country's in a java List or Map which you can iterate through in the JSP page and it is also reusable (OO) and which makes your life easy.

Cheers
Venu
 
You don't have the countries hard-coded, do you?

Doesn't your code look something like this:

Code:
<%
 while(getCountry.next())
 {
  String currCountry = getCountry.get(1);
%>
 <option name='<%=currCountry%>'><%=currCountry%></option>
<%
 }//end while
%>

If so, just make these additions:
Code:
<%
 while(getCountry.next())
 {
  [b]String selected = "";[/b]
  String currCountry = getCountry.get(1);
  [b]if(currCountry.equals(country))
   selected = "selected";[/b]
%>
 <option name='<%=currCountry%>' [b]<%=selected%>[/b]><%=currCountry%></option>
<%
 }//end while
%>

You put the selected variable in EVERY option, but only when it is appropriate will it actually equal the word "selected". Otherwise, it will just be a blank.

'hope I'm understanding your question correctly.

Dave


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
The problem is like this:

I have a string value retrieved from the database detailing the country this person last entered as their home country.

I have a drop down box (which they originally chose from and whose value was submitted into the database I now wish to retrieve from).

I want to highlight/select the same option this person submitted into the database.

e.g If first you filled in my form - with your name, John Dough, and for your country you select China (and submit and store in database). Next time you log in and want to amend your name/details and country, I want it to automatically display the name you orignally put and automatically select the country you submitted first time round.

I can get this value by performing <%String dbCountry = rs.getString("Country"); %>

But how do I get my select table to reselect this? I could compare the retrieved dbString with each of the hardCoded Select values (like I've done below with Afghanistan), but this is really time consuming if my select box has 100 countries.

<option value="Afghanistan" <%if (dbCountry.equals("Afghanistan")) {%> selected <%} %>>Afghanistan</option>


Hope I've explained it properly and thanks for taking an interest!~JW
 
Okay, I think I get that, but I'm still a little confused (possibly just because I'd go about something you're doing a different way).

Let's say you DIDN'T want to re-select the previously chosen option.

Are you simply hard-coding the list of countries into the drop-down list?

Code:
<option value="" >Pull down to select 
<option value="Afghanistan">Afghanistan</option>
<option value="Albania">Albania</option>
<option value="Algeria" >Algeria</option>
...etc...etc

That's an awful lot of countries! Why not put the countries in a database table and retrieve them in one result set, then you can built the options dynamically using the code like I recommend above.

If you absolutely have to list the countries via hard-code, then, right after the </select>, you could add:
Code:
<script>document.forms[0].country.value='<%=countryDB%>'</script>

Of course, not everyone will have JavaScript enabled, so this won't work for everyone. For those people, you have to do what you've got already (unless you put the countries in a database table, like I... and venur... recommend).

Dave


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
<script>document.forms[0].country.value='<%=countryDB%>'</script>

That's the baby! I was amending an existing page and didn't fancy creating a new table or array for 100 countries. Terrible I know, but much easier to copy and paste.

I appreciate this wasn't a JSP question in the end, but I knew you guys would know, thanks Dave.
 
I worked on a jsp profile page and had a similer problem. How I managed to get around that was the code below.
This is .java file I used.
Code:
package com.everbloom.beans;

import java.sql.*;
import java.util.*;
import com.everbloom.db.*;
import com.everbloom.membership.*;

public class Country extends User {

	private static Hashtable map;
	private static Vector list;
	private boolean active = false;

	public static Vector getList()
	{
		if(list != null)
			return list;
		java.sql.Connection conn = null;
		try
		{
			conn = DBFactory.getConnection();
			getList(conn);
		}catch(Exception e){
			throw new RuntimeException(e.getMessage());
		}finally{
			if(conn != null)
			{
				try
				{
					conn.rollback();
					conn.close();
				}catch(Exception e){
					e.printStackTrace();
					System.err.println("Could not close connection.");
				}
			}
		}
		return list;
	} 

	public static Vector getList(java.sql.Connection conn)
	{
	  if(map == null)
	  {
		try
		{
			map = new Hashtable();
			list = new Vector();
			ResultSet set = conn.createStatement().executeQuery("select * from Country");
			while(set.next())
			{
				Country e = new Country();
				e.setCountryName(set.getString("Country"));
				e.setCountryId(set.getInt("CountryId"));
				list.add(e);
			}
		}catch(Exception e){
			throw new RuntimeException(e.getMessage());
		}
	  }
	  return list;
	}
}
then on your jsp page you'll have code like this. A
Code:
<select size=1 name="CountryList">
	<option value="-1">-- Select --</option>
	<%
	Vector CountryList = Country.getList();
	for(int i = 0; i < CountryList.size(); ++i)
	{
		Country e = (Country)CountryList.elementAt(i);
		out.println("<option value=" + e.CountryId() + (e.CountryId() == *the value the user selected* ? " selected " : "") + ">");
		out.println(e.CountryName());
		out.println("</option>");
	}
	%>
</select>

This is similer to the code I have used on my pages although Im not sure if it is cut and paste freindly. You may need to modify some parts.

I don't know the answer but my good friend Google does.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top