• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do I resolve this? Adding loop item to a list

 
Pojahn Moradi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


When I click on the submit button, the specified item shoud be added to the item-list/cart. However, I am not getting the result I am expecting.
What is happening is that when the page have loaded, the "onclick" function have executed on all the items in the loop.

How do I resolve this?
What options do I have besides "onclick", which seems buggy?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65123
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pojahn Moradi wrote:What options do I have besides "onclick", which seems buggy?

onclick is not buggy; your code is.

You are mixing server-side code with client-side code; and the two cannot be mixed. Please read this article to understand how JSP operates and why what you are trying to do makes no sense.

You need to deal with client-side actions such as button clicks with JavaScript on the client; not JSP on the server.
 
Pojahn Moradi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right. It makes more sense now. Sill clueless on how to resolve this issue.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65123
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First item of advice: ditch the Java scriptlets. They've been obsolete for over 12 years and should no longer be used on JSP pages. Learn to use the JSTL and EL instead. See this JspFaq entry.

Advice #2: with your new knowledge of how JSP works, you'll need to sit back and rethink your approach. Your original approach assumed that you'd be able to add to the list in real time as buttons are clicked. Not so. To make any changes to the server, you need to go back with a new request -- either a link, a form or an Ajax request.

All of that requires a major refactor from the start. So don't try to band-aid what you've got; it's just going to lead to frustration. Sit back and think about how you can approach this knowing that once the JSP executes and sends the HTML to ther browser that you've got to wait for a new request to come back to do anything else.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic