• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

handling more than a form in a servlet and calling javascript function

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
I have 4 forms in the same servlet (New, Edit, View and Delete)
referencing each of them as the below with no issue (since all servlet should read the whole form)


for the delete action, i need to confirm the dialogue first so i have created a javascript for this.
but when i am adding the call onclick the form.action is no longer visibile to the code.


how can i call the javascript function and still point the action to the delete servlet and pass the ID along with the servlet?


 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That doesn't look like valid HTML, check all the quotes.

Anyway, why are you doing all of this in a Servlet? You should stick your layout in a JSP, and forward it the data you want to show after you've processed that in a Servlet.
 
Mo Alexwainy
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why should i transform to JSP? Cant a servlet do the work?
The issue is that if i remove the form.action from onclick the button will take the action of the previous one.
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, a Servlet can do all the work, but it will become incredibly hard to read and debug, and easy to introduce bugs, as you will see here.

You are writing JavaScript inside HTML inside Java. That is madness. Can you tell me which part of your delete form is the value of the onclick attribute?
 
Mo Alexwainy
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Value=delete not edit.
My copy was wrong.
Is there a way to reference the form and still call the js function onclick?
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, your onclick function doesn't do what you think it does, and it's because of issues I've pointed out in my previous posts. You're showing me Java code. Now lift the HTML out of the Java and see if it's valid. If it is, lift the JavaScript out of the HTML and see if it's valid.

If you find out what the problem is, there's a good way to prevent it from happening in the future. Separate your logic and your presentation.

Finally, I don't know why you're setting the form's action in JavaScript. It appears to me your different forms already have their action set.
 
Mo Alexwainy
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The html and js are working.
Would you please advise what would be the best practice to call a confirmation dialogue before executing the delete servlet noting that i need to pass a radio button value to the delete servlet?
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, <form> has an onsubmit event that you can cancel.

For now I've already given you advice on how to improve best practices in your code. Stop emitting HTML, CSS and JavaScript from Servlets. It would have made it easier to spot that your HTML is malformed:
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic