wood burning stoves*
The moose likes Struts and the fly likes show different content after a choice in a combobox Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "show different content after a choice in a combobox" Watch "show different content after a choice in a combobox" New topic
Author

show different content after a choice in a combobox

Wally Schnok
Greenhorn

Joined: May 31, 2001
Posts: 29
big lack of ideas : i have a
html:select combobox , i make a choice in and i need to show different content in the same page ,in base of my choice
<html:select property="optionField" onchange="refresh()">
<html ption value="noChoice"/>...
<html ption value="user"/>User
----etc -----etc ---etc ----
</html:select>
this combo has fixed values so can be designed in classic HTML too
i need an idea ... what to do ?!
refresh the hole page ? how to do it ? ( because i tried with javascript on the "onchange" event of the combo but i have no cache and no value of the parameters.... )
it sounds like a classic question but i'm in deep lack of ideas :-)
thank you.
<script type="text/javascript">
function refresh()
{
window.location.reload( false );
}
</script>
<%
out.println(request.getParameter("optionField"));
out.println(request.getAttribute("optionField"));
out.println(pageContext.getAttribute("optionField"));
//can't get values on this outs, it's NULL ever :-(((
%>
Matthew Marquand
Ranch Hand

Joined: Dec 19, 2002
Posts: 32
With complex interactions where a change in a select box requires dynamic data to be presented in some other part of the form, I usually try to "flash" the page. I do this by establishing an onchange value that will set the form's action location and then submit to the struts-defined action of my choice. That action looks at the selected value, obtains the desired dynamic data, puts it on the form, then forwards to the same view (jsp, tiles entry, ...) that you were on when you submitted.
Of course, there are time when flashing isn't allowed and more complex javascripty stuff has to done.
Example code:
Wally Schnok
Greenhorn

Joined: May 31, 2001
Posts: 29
it seems that calling the action with javascript on onchange doesn't work and it gives any javascript error !!!
(at less in my situation ).here's the code :
<html:form action="sendForm.do" method="GET" >
<html:select property="optionField" onchange="document.forms[0].action='sendForm.do'; document.forms[0].submit();">
<html ptions name="allOptions" />
</html:select>
<html:submit property="submit" value="send"/>
</html:form>
of course that pressing the submit button works correctly .
i tried calling a different action in onchange , with the same result :-((
Matthew Marquand
Ranch Hand

Joined: Dec 19, 2002
Posts: 32
What's the javascript error? Do you have multiple forms on the page?
Also, don't you want to POST instead of GET?
Wally Schnok
Greenhorn

Joined: May 31, 2001
Posts: 29
no , no multiple forms. and i have not any javascript error at all, but it does simply nothing !!!
Wally Schnok
Greenhorn

Joined: May 31, 2001
Posts: 29
i think that , generally , my initial question become simply like this :
how ( and if is possible ) can I start an Java action from some javascript ?!
this javascript
document.forms[0].action='myAction.do';
document.forms[0].submit();
it simply doesn't work to me
Matthew Marquand
Ranch Hand

Joined: Dec 19, 2002
Posts: 32
You'll have to POST the data into your target action to see the selection in the drop down.
Do you get into your action?
Do you have the same form associated with this new action as the target action for the submit?
Does the page actually submit and come back?
To help, I'll need more info.
Wally Schnok
Greenhorn

Joined: May 31, 2001
Posts: 29
so :
with my broser i point directly this page
http://localhost/index.jsp and i can see it correctly
index.jsp is like this
<html>
<% Vector v = new Vector();
v.add("...");
v.add("value1");
v.add("value3");
request.setAttribute("allOptions",v);
%>
<script type="text/javascript">
function reload()
{
document.forms[0].action='myAction.do';
document.forms[0].submit();
}
</script>
<html:form action="myAction.do" method="POST">
<html:select property="optionField"
onchange="reload()">
<html ptions name="allOptions" />
</html:select>
<html:submit property="submit" value="send"/>
</html:form>
</html>
ok, so I'm in this page . if i click submit ,I have myAction.do that starts and works correctly
I wanna have the same effect while changing the option in my combobox....
with the javascript above ( inspired by you)i have no effect and it gives me no javascript error , no submit, no nothing ....
hope i was clear now . :-)
my system : Server : OC4J last version
Struts 1.1 b2
Winnt 4
internet explorer 5
thnx for the patience and for any idea :-)
Matthew Marquand
Ranch Hand

Joined: Dec 19, 2002
Posts: 32
It's because of the submit button on the form. The javascript wants to submit the form using forms[0].submit() but that signature refers to the button called "submit" and not the submit method on the form. If you want to have a submit button on the form you can "click" the button by using "forms[0].submit.click()".
I don't know what your goal is but I hope this helps.
Wally Schnok
Greenhorn

Joined: May 31, 2001
Posts: 29
Nope , it's not that . i completly eliminate the submit button from my form and retry the onchange event. and does nothing , like usual :-((
i think that simply WE CAN'T start an action from javascript, so this will never work !!!
do you have some <b>functional</b> example of a form and a javascript associated with an event in the form and that calls an action and that you're sure it works ?!
maybe this one could give me some more inspiration
i repeat, i think it's impossible :-))
Matthew Marquand
Ranch Hand

Joined: Dec 19, 2002
Posts: 32
We submit to actions all the time using the previously described code.
I took your code, and ran it yesterday and it submitted fine. Here is the code exactly as it worked:

Do you have active scripting disabled in your browser options? When I turn active scripting off, I see the behavior your seeing. That setting also inhibits any scripting errors which I was seeing when the script was submit() and not submit.click(). Does other javascript work in your application?
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
What does the source sent to the browser look like?


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Wally Schnok
Greenhorn

Joined: May 31, 2001
Posts: 29
IT WORKS !!!
the unique change was to write all the script directly on the onchange event
and eliminate the javascript function that i used to call
crazy !!! :-///
anyway , i finally did it , thanx for all ....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: show different content after a choice in a combobox
 
Similar Threads
storing values chosen from dropdown
value submit html:select
i refresh after an onchange event on a combobox and i need the parameters in request
submission problem
how to access combo-box value in javascript?