Issue
I have a form in which i have two buttons one is save
and other is save as draft
, so on save i am submiting the form to other servlet
and on save as Draft
click i want to give action to new servlet class
- I am doing
<button type="submit" id="saveDraft" formaction="InsertAsDraft"">
,but its not working
<div class="container" id="divHide">
<form action="InsertQuantityIndent" method="post" id="indentForm" autocomplete="on">
<div class="row position-relative">
<div class="col-lg-4 brder">
<h5 id="commonHeader">Outlet Name</h5>
<select class="test" id="outlet" name="outlet">
<option>All</option>
<option>ol1</option>
<option>ol2</option>
</select>
</div>
<div class="col-lg-4">
<h5 id="commonHeader">Category</h5>
<select class="test" id="CategoryName" name="categoryCode">
<option>All</option>
<option>Cat1</option>
<option>Cat2</option>
</select>
</div>
</div>
<hr style="border: 1px solid black">
<div>
<button type="submit" id="save" class="commonButton">
<i class="fas fa-save"></i> Save
</button>
<button type="submit" id="saveDraft" formaction="InsertAsDraft" class="commonButton">
<i class="fas fa-save"></i> Save as draft
</button>
</div>
</form>
</div>
when i click on save the form gets submitted to InsertQuantityIndent
, but when i click on save as draft
it gets submitted but no data gets to back end
Like request.getparameter
in servlet
Here is my servlet
String outlet = request.getParameter("outlet");
String CategoryCode= request.getParameter("categoryCode");
System.out.println("outlet in new file :"+outlet);
here it print nothing, so i want to submit form on button click and get that data to back end bye its name
Solution
Here you can name buttons and check which value for button came and process accordingly.
<button type="submit" id="save" name="btn_save" class="commonButton">
<i class="fas fa-save"></i> Save
</button>
<button type="submit" id="saveDraft" name="btn_save_draft" class="commonButton">
<i class="fas fa-save"></i> Save as draft
</button>
or you can change type of button to button and handle click listener. From your listener change your form action and submit it using javascript.
Answered By - manj1790