?
Solved

Reloading option box

Posted on 2003-12-11
18
Medium Priority
?
313 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
LVL 30

Accepted Solution

by:
Mayank S earned 400 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

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

New style of hardware planning for Microsoft Exchange server.
The top devops trends for 2017 are focused on improved deployment frequency, decreased lead time for change and decreased MTTR.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

762 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