Solved

Reloading option box

Posted on 2003-12-11
18
307 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
xjc and jaxb 2 93
iterator example remove 8 74
How to find missing packages when using Netbeans IDE 8.1 ? 19 54
spring JDBC Template example error 26 243
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

809 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