aspose file tools*
The moose likes JSP and the fly likes select element as parameter for query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "select element as parameter for query" Watch "select element as parameter for query" New topic
Author

select element as parameter for query

m brymer
Ranch Hand

Joined: Jan 04, 2005
Posts: 64
Hi,
I have 3 select boxes on my jsp page box1=region, box2=country based on selection in box1 and box3=what is in database based on selection in box1. My question is, is there a way to grab the selection from box1 and put it into the where clause of a sql statement???
Thanks!
[ February 23, 2005: Message edited by: Bear Bibeault ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61433
    
  67

When the form is submitted the value of the select element is sent as a request parameter just like any other. Are you having a specific issue?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
m brymer
Ranch Hand

Joined: Jan 04, 2005
Posts: 64
I don't want to have to re-post the page if at all possible. I just want to be able to read what is selected in the first box and take that selection and plug it into the where clause of my sql query to the database to populate the 3rd box. Is this doable?
Eugene Lucash
Ranch Hand

Joined: Feb 19, 2005
Posts: 77
Surely you can populate select box without rePOSTing, but it is non trivial task.

you must do the following things but I will not describe details.
Actually, I want to scare you out of such kind a solution.

1. create separate page, which create output based on parameter in box1
Query the database, get ResultSet, and dynamycally write output like
following.
let it be /box3pop.jsp?param=x, where x is value of select box1

<html>
<head>
<script type="text/javascript">
window.on load = function () {
parent.document.getElementById('idbox3').innerHTML=document.body.innerHTML
}
</script>
</head>
<body> <!--output is based on a value of x-->
<option value="22">sdscd</option>
<option value="44">ttyy</option>
<option value="91">zzzzzz</option>
</body>
<html>


2. insert hidden iframe in your form.

<select onchange="populateBox3(this)" name="box1">
<option>sds</option>
......
</select>
.......
<select id="idbox3" name="box3">
<option></option>
</select>

<iframe name="hidFrame" style="display:none"></iframe>

3. insert javascript in page your page original, first page

function populateBox3(selectBox1) {
var hiddenFrame = document.frames['hidFrame'];
hiddenFrame.src='/box3pop.jsp?param=' + selectBox1.value;
}


.....
Cool, heh?
Regards
[ February 23, 2005: Message edited by: Eugene Lucash ]
m brymer
Ranch Hand

Joined: Jan 04, 2005
Posts: 64
Thanks Eugene! But I think I am going to go with a preparedStatement if I can only get it to read the selection in the select box
Eugene Lucash
Ranch Hand

Joined: Feb 19, 2005
Posts: 77
Take a look at another solution, but with postbacks
http://www.coderanch.com/t/115659/HTML-JavaScript/Select-Box-one-page-second

post #6
m brymer
Ranch Hand

Joined: Jan 04, 2005
Posts: 64
Am I forced to do post back? Won't this work?

Eugene Lucash
Ranch Hand

Joined: Feb 19, 2005
Posts: 77
The main difference is in what you want.
If you want box1 and box3 on the same page,
so than box3 populated dependantly on box1 value
you need postback or some javascript stuff
(including separate hidden server request) to repopulate box3.

If you can place box1 on one page,and after submiting
you respond with the second page where box3 placed (in a wizard maner)
then you don't need any extra stuff.
m brymer
Ranch Hand

Joined: Jan 04, 2005
Posts: 64
Yes, I want both on the same page. I already have an onchange event for box2, I guess I will also have to write one for box3. Thanks for your time.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I think you might not have a full grasp on what gets executed on the server and what gets executed on the client.

Onchange events happen on the client (the users browser). The only way to use the values in a select box for your JDBC statement is to post the information back to the server.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: select element as parameter for query