how to submit a form without using a submit button in servlet
if(Category.equals("Seasonal Decorations")){
out.println("<form name='adddeco' id='adddeco' method='post'>");
out.println("<input type=hidden name=cater value=decos>" +
"<table border=0 colspan=5 >" +
"<tr><th>Name & Description of DECORATION </th><th><input type=text name=deco_itemname width=30></th></tr>");
out.println("<tr><th>Product ID </th><th><input type=text name=deco_PID width=30></th></tr></table>");
out.println("<tr><th>Provider </th><th><input type=text name=deco_provide width=30></th></tr>");
out.println("<tr><th>AVAILIBILTY</th><th><select name=DECO_AVAIL>"+
"<option>UNAVAILABLE</option>" +
"<option>AVAILABLE</option></select></th></tr>");
out.println("<tr><th>No.of DECORATION </th><th><input type=text name=deco_qnty width=30></th></tr>");
out.println("<tr><th>Shelf NO </th><th><input type=text name=deco_shelfno width=30></th></tr>");
out.println("<tr><th>Original Price </th><th><input type=text name=deco_orgprice width=30></th></tr>");
out.println("<tr><th>Selling Price </th><th><input type=text name=deco_sellprice width=30></th></tr>");
out.println(" <input type=button value=add onclick='$(#adddeco).attr(action,additem1);$(#adddeco).submit();'> <input type=reset value=clear>" +
"</form>");
The above is my code in a Servlet in order to enter a value to a DB but the problem is that I'm not able to sumbit this form to the next page as you can see I'm not using a "submit" button instead I'm using an event onclick=''. The above code works in JSP if the "out.println();" is removed but as you can see that I need to compare category.equals("seasonal decorations")
and run on the specific code only when required so if anyone who knows about how to overcome this particular problem please 开发者_开发百科help me out.
And please keep in mind that I cannot use a sumbit button to submit as I'm using a barcode scanner.
Avoid writing out HTML from a Servlet, it's bad practice. It's hard to get right, it's hard to maintain and the readability is abysmal. This is a job best suited for JSPs.
Your problem appears to be that the JavaScript being generated is invalid. If you check the source of the output page, you'll notice that you're printing out:
<input type=button value=add onclick='$(#adddeco).attr(action,additem1);$(#adddeco).submit();'>
Note that there are no quotes around the argument to $()
. There should be.
This is closer to correct (I say closer because your HTML may be invalid too, depending on your DOCTYPE
- you don't close the tags and you don't have quotes around attributes):
out.println("<input type=button value=add onclick='$(\"#adddeco\").attr(action,additem1);$(\"#adddeco\").submit();'> <input type=reset value=clear>"
All this escaping of quotes is a big reason why you should use JSPs instead.
You should probably read about and use JSTL to make your code more readable. It allows you to use if statements and other control structures without resorting to Java code. Example:
<c:if test="category eq 'Seasonal Decorations'">
<form name="adddeco" id="adddeco" method="post">
...
</form>
</c:if>
精彩评论