Solved

Reloading option box

Posted on 2003-12-11
18
301 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:mayankeagle
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:mayankeagle
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:mayankeagle
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:mayankeagle
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:mayankeagle
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 30

Accepted Solution

by:
mayankeagle 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:mayankeagle
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:mayankeagle
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:mayankeagle
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:mayankeagle
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Learn to move / copy / export exchange contacts to iPhone without using any software. Also see the issues in configuration of exchange with iPhone to migrate contacts.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

705 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

13 Experts available now in Live!

Get 1:1 Help Now