Solved

Reloading option box

Posted on 2003-12-11
18
304 Views
Last Modified: 2010-04-01
Hi,

I have a JSP page with an option selection box.
The problem is, when i select an item in the box, it refreshes the page therefore reseting my selection.

i have the following code that moves the selected item to the top of the selection box, therefore when refreshed it is the item selected.

I know the <option selected>Item</option> code. but i Apache Tomcat crashed when i try to compare strings.
Here's my current code:

<td>Country: </td>
<td><form name="form1" method="post" action="create.jsp">
<select name="Country" id="Country">

<%
      while(rs.next())
      {
            out.println("<option>" + rs.getString("CountryName") + "</option>");
      }

      out.println("</select></td>");
      out.println("</tr>");

      rs.close();
      con.close();
      statement.close();

      //Load the Driver class file
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

      //Make a connection to the ODBC datasource Movie Catalog
      con = DriverManager.getConnection("jdbc:odbc:SpotlightMarketingCalendar","", "");

      //Create the statement
      statement=con.createStatement();

      //User the created statement to Select the data
      // RFrom the titles table
%>
0
Comment
Question by:sexy-sx
  • 10
  • 8
18 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 9926232
Why is it supposed to refresh the page after selecting an option? Can that not be avoided? Are you using a Javascript function to do that onChange of the selection? Otherwise, use hidden variables and set their values.... when the page reloads, check the value of the hidden-variable.... if it matches with the option, then make it selected.

Mayank.
0
 

Author Comment

by:sexy-sx
ID: 9926247
When the user selects the country, there's another option selection box that will display the events for the country selected.

so i have to refresh the form to populate the event selection box with events for that country.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 9926368
Then use a hidden-variable in the form.

<input name = "hidCountry" value = "<%= request.getParameter ( "hidCountry" ) != null ? request.getParameter ( "hidCountry" ) : "" %> >

And onChange of the selection in the combo-box, call a Javascript function, like:

<select name="Country" id="Country" onChange ="javascript:onCountryChange ()">

For every option here, check if its the one which was selected initially:

String sCountryName = rs.getString("CountryName") ;
String sParameter = request.getParameter ( "hidCountry" ) ;
out.println ( "<option " + ( sCountryName.equalsIgnoreCase ( sParameter ) ? "selected" : "" ) + " > " + sCountryName + "</option>" ) ;

In the javascript function, do something like:

function onCountryChange ()
{
  document.form1.hidCountry.value = document.form1.Country.value ;
  document.form1.submit () ;

}

Mayank.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 9926375
>> value = "<%= request.getParameter ( "hidCountry" ) != null ? request.getParameter ( "hidCountry" ) : "" %>

I forgot the closing quote here:

value = "<%= request.getParameter ( "hidCountry" ) != null ? request.getParameter ( "hidCountry" ) : "" %>"
0
 

Author Comment

by:sexy-sx
ID: 9954102
that's not what i'm looking for. it's not working like i want it to.

i will paste in the code i'd like to work.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 9954562
Then please post your code.
0
 

Author Comment

by:sexy-sx
ID: 9954579
please bare with me as i have a lot of projects in my hands at the moment.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 9954628
Ok, take your time.
0
 

Author Comment

by:sexy-sx
ID: 9960487
Have a close look at my if statement?
I am a vb programmer, so i'm not too sure the logic or why this is going wrong.

sgetEventCountry = request.getParameter("Country");
String sgetEventTempCountry = "";

if(request.getParameter("Country") == null || request.getParameter("Country").equals(""))
{
      %>
      <option selected> </option>
      <%
      rs = statement.executeQuery("SELECT * FROM tbl_Country");
      while (rs.next())
      {
            out.println("<option>" + rs.getString("CountryName") + "</option>");
      }
}
else
{
      /* out.println("<option selected>" + sgetEventCountry + "</option>"); */

      rs = statement.executeQuery("SELECT * FROM tbl_Country where CountryName <> '" + sgetEventCountry + "'");
      while (rs.next())
      {
            sgetEventTempCountry = rs.getString("CountryName");

            if(sgetEventTempCountry.equals(sgetEventCountry)) <---- Here's the if statement that doesn't work can you tell
            {
                  out.println("<option selected>" + sgetEventTempCountry + "</option>");
            }
            else
            {
                  out.println("<option>" + sgetEventTempCountry + "</option>");
            }
      }
}

Thanks!
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 30

Accepted Solution

by:
Mayank S earned 100 total points
ID: 9961762
Always check for null strings, and prefer to trim the values:

>> sgetEventCountry = request.getParameter("Country") ;

sgetEventCountry = request.getParameter("Country") != null ? request.getParameter ("Country").trim () : "" ;

>> sgetEventTempCountry = rs.getString("CountryName");

sgetEventTempCountry = rs.getString("CountryName") ;
sgetEventTempCountry = sgetEventTempCountry != null ? sgetEventTempCountry.trim () : "" ;

And prefer using equalsIgnoreCase () while comparing parameters....

Mayank.

0
 

Author Comment

by:sexy-sx
ID: 9961837
still no go.

sgetEventCountry = request.getParameter("Country") != null ? request.getParameter ("Country").trim () : "" ;
String sgetEventTempCountry = "";

      rs = statement.executeQuery("SELECT * FROM tbl_Country where CountryName <> '" + sgetEventCountry + "'");
      while (rs.next())
      {
            sgetEventTempCountry = rs.getString("CountryName");
            sgetEventTempCountry = sgetEventTempCountry != null ? sgetEventTempCountry.trim() : "" ;

            if(sgetEventTempCountry.equalsIgnoreCase(sgetEventCountry))
            {
                  out.println("<option selected>" + sgetEventTempCountry + "</option>");
            }
            else
            {
                  out.println("<option>" + sgetEventTempCountry + "</option>");
            }
      }
}

does that look right?
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 9961992
Definitely looks right. What's the last }, though? Which closing brace is that?

And what error are you getting? What is happening when you run the code?
0
 

Author Comment

by:sexy-sx
ID: 9962011
i'm not getting an error, it's just doesn't show the selected country.

i have 3 coutries in that table:
Australia
New Zealand
Singapore

When i select Australia, the option box only shows New Zealand and Singapore.
When i select New Zealand, it only shows Australia, Singapore and etc...

So the if statement is matching but not printing the output i selected.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 9962106
Of course, it will :-)

Guess what the error is? If you can't, I'll tell you....
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 9962117
Check your query. You're selecting only those values from the database, which are not equal to the Country that was initially selected. You should select all values from the database. Because later, in the if () condition, you are checking whether the value read from the database is equal to the one which was selected or not, and then you are selecting that option.

Mayank.
0
 

Author Comment

by:sexy-sx
ID: 9962140
ha ha ha!!!

thanks mate, i changed to query to see if worked with something else and never got changed it back.

thanks heaps for all your help!
0
 

Author Comment

by:sexy-sx
ID: 9962143
by the way i'll increase the points to 100 just because you were such good help and patient
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 9962229
Thanks. Glad to help.

BTW, probably what you wanted to do through that query was: first select the value (in another query) which was selected, and populate the combo-box with that value. And then shoot another query which selects the remaining values and populates the rest of the combo-box (so they always come at the bottom)?

You could also have done it by simply populating the combo-box with the selected value directly:

out.println("<option selected>" + sgetEventCountry + "</option>");

And then select the rest of the value from the database (using the same query which you were using), and then simply populating the combo-box with them (not bothering to check which one was selected).

Mayank.

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
parse example 13 93
'exit' is not valid keyword in java 14 148
own marker interface 1 50
Books that can get me started on JAVA 2 81
There are many Password Managers (PM) out there to choose from. PM's can help with your password habits and routines, but they should not be a crutch you rely on too heavily. I also have an article for company/enterprise PM's.
When we talk about DevOps toolchains, I sometimes wonder how many people really get what we’re talking about. I don’t know if it’s just semantics or tone or something else, but sometimes I think it just sounds like buzzword sausage. So it’s always …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now