Link to home
Start Free TrialLog in
Avatar of WarAngelos
WarAngelos

asked on

Java (Select Box) OnChange - Not JavaScript

I need to able to retrieve some values and place them in another select box(parameter_pool_time)
basically when you choose from the select box session type i want it to do a onchange and get me the values in SQL Database i have linked

I am totally clueless as to where to start, please help me i really need this.

i have done this in C# and Access Database before but this is totally different i have no clue how id do it in Java. please help me i have really less time left.

PS: Remember Java not JavaScript.
out.println("<TR><TH>Session Type</TH><TH>Day</TH><TH>Click Button to get times</TH></TR><TR><TD><select name=\"parameter_pool_sessiontype\"><option value=\"Members Swimming\">Members Swimming</option><option value=\"Aqua Aerobic\">Aqua Aerobic</option><option value=\"Adults Only\">Adults Only</option><option value=\"Aqua Deep End Swim\">Aqua Deep End Swim</option><option value=\"Splash Time\">Splash Time</option></select></TD>");

Open in new window

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You need to activate a form submission via javascript that performs the *java* at the server side
Avatar of WarAngelos
WarAngelos

ASKER

roughly how do i do that?
Which bit, the javascript or the java?
the javascript bit please
If this helps this is the whole thing i have

this is incomplete i am using it form another of my pages and working top to down i have not really reached the try/catch part yet I am stuck on how to do onchange.
package dh101;
 
 
 
import java.io.*;
 
import java.util.*;
 
import java.sql.*;
 
 
 
import javax.servlet.*;
 
import javax.servlet.http.*;
 
 
 
 
 
public class Pool extends HttpServlet
 
{ 
 
    public void doGet (
 
			HttpServletRequest	request,
 
			HttpServletResponse	response
 
		) throws ServletException, IOException
 
		{
 
 
 
		PrintWriter	out;
 
		String Title = "Pool Bookings";
 
		String parameter = "";
 
		String pool_username = "";
		String pool_time = "";
		String pool_day = "";
		String pool_sessiontype = "";
 
		String sql = "";
 
		Connection dbcon = null;
 
		Statement stmt = null;
 
		ResultSet rs = null;
 
 
 
		if( (parameter = request.getParameter("parameter_pool_time")) != null) 
 
		  pool_time  = parameter;
 
		if( (parameter = request.getParameter("parameter_pool_day")) != null) 
 
		  pool_day  = parameter;
		if( (parameter = request.getParameter("parameter_pool_sessiontype")) != null) 
 
		  pool_sessiontype = parameter;
 
 
 
 		// set content type 
 
		response.setContentType("text/html");
 
		// then write the data of the response
 
		out = response.getWriter();
 
		out.println("<HTML><HEAD><TITLE>" + Title + "</TITLE></HEAD>");
 
		out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"dh101.css\" />");
		out.println("<BODY><CENTER><H1>" + Title + "</H1>");
 
		out.println("<img height=450 width=700 src=\"images/pool.jpg\">");
 
		out.println("<H3>Pool Booking Form</H3>");
 
		out.println("<form>");
		out.println("<TABLE>");
		out.println("<TR><TH>Personal Details</TH></TR>");
 
		out.println("<TR><TH>Session Type</TH><TH>Day</TH><TH>Click Button to get times</TH></TR><TR><TD><select name=\"parameter_pool_sessiontype\" onchange=\"function()\"><option value=\"Select\">Select</option><option value=\"Members Swimming\">Members Swimming</option><option value=\"Aqua Aerobic\">Aqua Aerobic</option><option value=\"Adults Only\">Adults Only</option><option value=\"Aqua Deep End Swim\">Aqua Deep End Swim</option><option value=\"Splash Time\">Splash Time</option></select></TD>");
 
		out.println("<TD><select name=\"parameter_pool_day\"><option value=\"Select\">Select</option><option value=\"Monday\">Monday</option><option value=\"Tuesday\">Tuesday</option><option value=\"Wednesday\">Wednesday</option><opjavation value=\"Thursday\">Thursday</option><option value=\"Friday\">Friday</option><option value=\"Saturday\">Saturday</option><option value=\"Sunday\">Sunday</option></select></TD>");
		out.println("<TR><TH></BR>Booking Details</TH></TR>");
 
		out.println("<TR><TH>Time</TH><TR><TD><select name=\"parameter_pool_time\"><option value=\"B\">B</option></select></TD>");
 
		out.println("</TR></TABLE></BR>");
		out.println("<input type=submit>");
 
		out.println("</form>");
 
		try 
 
		{
 
			// Load the JDBC-ODBC driver
 
			Class.forName("com.mysql.jdbc.Driver").newInstance();	
 
			dbcon = DriverManager.getConnection("jdbc:mysql://localhost/test");
 
			stmt = dbcon.createStatement();
 
			//insert data into event database table
 
			if(!pool_username.equals("") && !pool_time.equals("") && !pool_day.equals("") && !pool_sessiontype.equals("") )
 
			{
 
				//sql = "INSERT INTO pool VALUES ('" + pool_firstname + "','" + pool_lastname + "','" + pool_start + "','" + pool_end + "','" + pool_date + "','" + pool_sessiontype + "')";
 
				// Load the JDBC-ODBC driver and execute SQL statement
 
				//stmt.executeUpdate(sql);
 
			}
 
			
 
			//read data from database table
 
			rs = stmt.executeQuery("SELECT * FROM pool");
 
			out.println("<TABLE BORDER=1>" );		
 
			out.println("<TR><TH>First Name</TH><TH>Last Name</TH><TH>Start Time</TH><TH>End Time</TH><TH>Date</TH><TH>Session Type</TH></TR>" );		
 
			while ( rs.next() )
 
			{
 
				/*out.println("<TR><TD>" +  rs.getString("pool_firstname") + "</TD>" );
				out.println("<TD>" +  rs.getString("pool_lastname") + "</TD>" );
 
				out.println("<TD>" +  rs.getString("pool_start") + "</TD>" );
 
				out.println("<TD>" +  rs.getString("pool_end").toString() + "</TD>" );
				out.println("<TD>" +  rs.getString("pool_date") + "</TD>" );
				out.println("<TD>" +  rs.getString("pool_sessiontype") + "</TD>" );*/
 
			}
 
			out.println("</TR></TABLE>" );				
 
			stmt.close();
 
			dbcon.close();
 
		}		
 
		 catch( Exception e ) 
 
		{ 
 
            e.printStackTrace(out);
 
 
 
		}//catch	
 
		out.println("</BODY></HTML>");
 
		out.close();
 
			
 
  }
 
}
 
//end/////////////////////////////////////////////////////////////////////////////////////////////////////////

Open in new window

Are you sure about this onchange thing? I would advise against it and make them click explictly or you'll find that you'll be handling more queries than you (or they) need
Please i have no other choice i have a TEST 2morrow Network Software then i have to work Friday Night Shift and Saturday Night Shift Sunday University is closed library does not have linux and Monday i need to Hand this in am totally paniking right now.
Basically what i want is if your look at the code there are 3 Select Boxes... 2 of them are preloaded... when they are selected i want it to do the onchange thing then check compare the two value is the thing and get the times and put them in the 3rd select box.
OK. Here's an onchange example

http://www.java2s.com/Code/JavaScriptReference/Event-Handlers-Reference/onChangeExample.htm

In your case, all you need in 'function1' is
document.forms[0].submit();

Open in new window

but how would i connect it to the sql i am really sorry for being a pain... basically i have sql totally figure out in java but in javascript i have not got a clue how to access it and stuff
nvm thank you soo much i deffinetly owe you one... i have got it man thanks
Just one more thing once i get to sql how to i replace the stuff in select box i know whats going on now its submiting hence activating the try and catch but in there i know how to retrieve the details but how to do add it to the SELECT BOX just this one please tell me thank you.
That's it. It's the java side that does the SQL - nothing to do with the javascript. JS just submits the form
>>Just one more thing once i get to sql how to i replace the stuff in select box

Well i don't know what your application logic is but basically all you do is rewrite the html (and hence the select element) based on what the query returns
Hmm sorry but err right for e.g. when i say choose members swimming and monday

sooo in sql i would be like SELECT SESSION, DAY, TIME FROM SWIMMING WHERE SESSION = '"+SELECTBOX1+"'" and the other select box...

write so it gets me the stuff but it would not change the 3rd select box on its own thats for sure i need the code to put these results into the SELECT BOX.
I dunno how to add not even a clue on how to start

I hope you get me.
You need to iterate the ResultSet and get the values you need from which you can rewrite the html. What purpose to your app does the third select serve?
basically this is pool booking site...

its not a app....its a webpage

Session Type(SELECT BOX 1) and DAY(SELECT BOX 2)

With them I want the time for these two togeather for e.g. session members swimming and day monday
with these two i would like the time of session members swimming available on monday
and insert these times into TIME(SELECTBOX 3) soo the user may choose the time and then place a booking.
OK, so what you should do is iterate the ResultSet and get the times and put them in the 3rd select. By 'app' i mean of course 'webapp', however it happens to be deployed
yeah that is what i want to do i no how to use resultset but how do i insert these values into the 3rd select thats the question i have sorry for being a pain... i appreciate that you have not ditched me so far or i would have thought some one else would have left me ages back LOL
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Will this add even if the there are multiple members swimming sessions basically multiple times more than 1? thank you so much ^^
It will loop through any and all times present in the ResultSet
Okay Thank you very much for you help i really appreciate it... I have to leave campus now the build is closing only library's stay open 24/7.. thank you again.

Ill give the code a go thank you very much.
OK :-)
Thanks a lot you were really helpful thank you.