File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes Switching form action based upon radio selection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Switching form action based upon radio selection" Watch "Switching form action based upon radio selection" New topic
Author

Switching form action based upon radio selection

Apple Khoa
Ranch Hand

Joined: Jul 27, 2011
Posts: 37
Hi all,

Does anyone know if it is possible for a webpage to be mapped to several servlets?

For instance, is it possible for my webpage (shown below) to 'call' a servlet, say EasyQnServlet, when a user chooses the radio button 'Easy' and another servlet, say DifficultQnServlet, when the option 'Difficult' is selected?



An advance thank to all
Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 247

yes you can attain using AJAX... Using "OnClick" event on Radio button, Toggling of URL should be done correspondingly based on option selected...


SCJP 6 [SCJP - Old is Gold]
Apple Khoa
Ranch Hand

Joined: Jul 27, 2011
Posts: 37
Ram Narayan.M wrote:yes you can attain using AJAX... Using "OnClick" event on Radio button, Toggling of URL should be done correspondingly based on option selected...


Hi Ram Narayan.M,

Thank you for your prompt reply.

I will take a look at the ajax examples available

Thanks for your help once again.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61221
    
  66

Ajax is not necessary. Just a little JavaScript to change the action of the form based upon what element is selected.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Apple Khoa
Ranch Hand

Joined: Jul 27, 2011
Posts: 37
Bear Bibeault wrote:Ajax is not necessary. Just a little JavaScript to change the action of the form based upon what element is selected.


Hi Bear Bibeault,

May I know what do you mean by "Just a little JavaScript to change the action of the form based upon what element is selected"?
Could you kindly provide me with some examples?

By the way, I have tried using Ajax as suggested by Ram Narayan.M. However, I guess I have done something wrong somewhere as I am not getting what is desired. I will be explaining the issue in my next post (as I will be quoting Ram Narayan.M). Do you mind advising me on where my mistakes could be?

An advance thank for your help
Apple Khoa
Ranch Hand

Joined: Jul 27, 2011
Posts: 37
Ram Narayan.M wrote:yes you can attain using AJAX... Using "OnClick" event on Radio button, Toggling of URL should be done correspondingly based on option selected...


Hi Ram Narayan.M,

I have tried using AJAX to map my webpage to different servlets based on the option selected by the user. However, I guess I have done something wrong somewhere as the output is not what as desired. My guess is that the AJAX is not responding to the 'onclick' event for the radio buttons. Could you kindly advise me on this issue?

My html code:


My web.xml code:


The pdf file generated contains only the title and the instructions as texted in by the users. The 'question' and 'answer' are not reflected in the output.

By the way, I am using doGET method in my servlet.

Is the "get" method used in my html script the possible cause? (I saw an example on a website using POST method.)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61221
    
  66

Apple Khoa wrote:
May I know what do you mean by "Just a little JavaScript to change the action of the form based upon what element is selected"?

You can write one or two lines of JavaScript to establish an event handler for the change of selection and replace the action value accordingly.

By the way, I have tried using Ajax as suggested by Ram Narayan.M

That's like using nuclear weapons to crack walnuts.

Ajax is a powerful and useful tool that is completely unnecessary for this issue.
Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 247

If you are trying to change "action" attribute in form, then you can go with the way Bear Bibeault suggested .. no need of AJAX...

If you are intending to display some dynamic large forms based on option selected, use AJAX in that aspect...

Actually, What will be displayed in the "message"?...

I cant see form named with "MyForm"... Please check the HTML Prgm again...
Apple Khoa
Ranch Hand

Joined: Jul 27, 2011
Posts: 37
Bear Bibeault wrote:You can write one or two lines of JavaScript to establish an event handler for the change of selection and replace the action value accordingly.


Hi Bear Bibeault,

I have tried using an event handler onclick="location.href='/EasyQnServlet'" (Line 36 of HTML code) & onclick="location.href='/EasyQnServlet'" (Line 37) to perform the task of mapping to several servlets with one webpage. However, I am still not getting the desired output. The content for 'question' and 'answer' (radio buttons) are not being displayed; only the content 'title' and 'instruction' (text boxes) appear on the output.

Could you kindly advise me on this issue?

My HTML code:


My web.xml code:
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61221
    
  66

You might want to fix the quoting in your reply.

In any case, just post the relevant parts of the code or an SSCCE. I'm not going to look at code that covers more than one screen to find the one line of JavaScript that's relevant.

By the way, when it comes to event handling, you really want to use jQuery or similar. Otherwise, you've got to deal with IE's nonsense.
Apple Khoa
Ranch Hand

Joined: Jul 27, 2011
Posts: 37
Bear Bibeault wrote:You might want to fix the quoting in your reply.

In any case, just post the relevant parts of the code or an SSCCE. I'm not going to look at code that covers more than one screen to find the one line of JavaScript that's relevant.

By the way, when it comes to event handling, you really want to use jQuery or similar. Otherwise, you've got to deal with IE's nonsense.


My HTML code (shortened)


Used <form method="get" action="simul"> in the html.

My web.xml code (shortened)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61221
    
  66

No, you're causing a redirect, not submitting the form. and that's not what you do either, is it?

Using jQuery, here's something off-the-cuff (not guaranteed to be 100% correct). In the ready handler:

If you don't want to use jQuery, it'll take a bit more code, but this should give you the idea.

You don't want the change of radio value to trigger a redirect or a submission, you just want it to switch out the action, right?

Of course, it might just be easier to handle it on the server by make the servlet do the work based upon the submitted value of 'difficulty'.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61221
    
  66

At this point this is all about JavaScript and not servlets, so I've changed the title a bit and moved it to the HTML forum.
Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 247

What is your plan to render on toggling Easy and Difficulty mode?...

Meanwhile In,

<

location.href cannot be invoked in this manner... It should be embodied in a function and have to call that function... But, using location.href will redirect to the next page...

And you cant get the effect of actions based on toggling effect happening in the same page...

Apple Khoa
Ranch Hand

Joined: Jul 27, 2011
Posts: 37
Ram Narayan.M wrote:What is your plan to render on toggling Easy and Difficulty mode?...

Meanwhile In,

<

location.href cannot be invoked in this manner... It should be embodied in a function and have to call that function... But, using location.href will redirect to the next page...

And you cant get the effect of actions based on toggling effect happening in the same page...



My Mathematics webpage has a form which has two options (radio buttons) for the users - Easy Question or Hard Question

If the user selects 'Easy Question', the webpage has to call the EasyQnServlet (with 'easy questions' mathematical logic in servlet).
If the user selects otherwise, the webpage has to call the HardQnServlet.

Everything is working fine when my webpage maps only to ONE servlet. My issue now is how to make my webpage maps to the relevant servlet according to what the user chooses (Easy / Hard).

By the way, output is set as pdf file in my servlet, not HTML.

(I am not using any database.)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61221
    
  66

If the JavaScript is beyond you at this point, I'd just make the switch on the server.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Switching form action based upon radio selection