Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

select element as parameter for query

 
m brymer
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
m brymer
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am I forced to do post back? Won't this work?

 
Eugene Lucash
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic