• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 315
  • Last Modified:

Reloading option box

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
sexy-sx
Asked:
sexy-sx
  • 10
  • 8
1 Solution
 
Mayank SAssociate Director - Product EngineeringCommented:
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
 
sexy-sxAuthor Commented:
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
 
Mayank SAssociate Director - Product EngineeringCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Mayank SAssociate Director - Product EngineeringCommented:
>> value = "<%= request.getParameter ( "hidCountry" ) != null ? request.getParameter ( "hidCountry" ) : "" %>

I forgot the closing quote here:

value = "<%= request.getParameter ( "hidCountry" ) != null ? request.getParameter ( "hidCountry" ) : "" %>"
0
 
sexy-sxAuthor Commented:
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
 
Mayank SAssociate Director - Product EngineeringCommented:
Then please post your code.
0
 
sexy-sxAuthor Commented:
please bare with me as i have a lot of projects in my hands at the moment.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Ok, take your time.
0
 
sexy-sxAuthor Commented:
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
 
Mayank SAssociate Director - Product EngineeringCommented:
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
 
sexy-sxAuthor Commented:
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
 
Mayank SAssociate Director - Product EngineeringCommented:
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
 
sexy-sxAuthor Commented:
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
 
Mayank SAssociate Director - Product EngineeringCommented:
Of course, it will :-)

Guess what the error is? If you can't, I'll tell you....
0
 
Mayank SAssociate Director - Product EngineeringCommented:
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
 
sexy-sxAuthor Commented:
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
 
sexy-sxAuthor Commented:
by the way i'll increase the points to 100 just because you were such good help and patient
0
 
Mayank SAssociate Director - Product EngineeringCommented:
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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 10
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now