最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to make submit and save button work on jsp page? - Stack Overflow

programmeradmin0浏览0评论

I am creating a form..i want my form to have two buttons one for "submit" and the other for "Save as draft".when I click on submit the data has to be submitted and when I click on save it should show the filled form on another page.I know how to submit and save independently but how do I make them work in the same form.my code is as follows

<form method="post" action="submitpage.jsp" >//I want here in action to change according to the button clicked
<table border="2" >
<tr>
<td>Field 1</td>
<td><input  type="text" style=" width: 150px; height: 15px" value="" name="Field1"  id="F1"/></td>
</tr>
<tr>
<td>Field 2</td>
<td><input  type="text" style=" width: 150px; height: 15px" value="" name="Field2"  id="F2" /></td>
</tr>
<tr>
<td>Field 3</td>
<td><input  type="text" style=" width: 150px; height: 15px" value="" name="Field3"  id="F3" /></td>
</tr>
<td>Field 4</td>
<td><input  type="text" style=" width: 150px; height: 15px" value="" name="Field4"  id="F4" /></td>
</tr>


<tr>
<td></td>
<td><input type="submit" value="Submit" onclick="return js_file_for_validation()" autofocus="autofocus"/></td>
<td><input type="button" value="Save as draft" /></td>
</tr>
</table></form>

</form>

P.s:-I AM USING JAVASCRIPT FOR VALIDATING SUBMIT!!!! IS there anything that I have to write in js file ??? Guidance will be helpful!!

Thanks in adv!!

I am creating a form..i want my form to have two buttons one for "submit" and the other for "Save as draft".when I click on submit the data has to be submitted and when I click on save it should show the filled form on another page.I know how to submit and save independently but how do I make them work in the same form.my code is as follows

<form method="post" action="submitpage.jsp" >//I want here in action to change according to the button clicked
<table border="2" >
<tr>
<td>Field 1</td>
<td><input  type="text" style=" width: 150px; height: 15px" value="" name="Field1"  id="F1"/></td>
</tr>
<tr>
<td>Field 2</td>
<td><input  type="text" style=" width: 150px; height: 15px" value="" name="Field2"  id="F2" /></td>
</tr>
<tr>
<td>Field 3</td>
<td><input  type="text" style=" width: 150px; height: 15px" value="" name="Field3"  id="F3" /></td>
</tr>
<td>Field 4</td>
<td><input  type="text" style=" width: 150px; height: 15px" value="" name="Field4"  id="F4" /></td>
</tr>


<tr>
<td></td>
<td><input type="submit" value="Submit" onclick="return js_file_for_validation()" autofocus="autofocus"/></td>
<td><input type="button" value="Save as draft" /></td>
</tr>
</table></form>

</form>

P.s:-I AM USING JAVASCRIPT FOR VALIDATING SUBMIT!!!! IS there anything that I have to write in js file ??? Guidance will be helpful!!

Thanks in adv!!

Share Improve this question asked Feb 21, 2014 at 5:24 user3300851user3300851 871 gold badge3 silver badges12 bronze badges 2
  • Use an if statement,isnt that obvious?when u press submit do this or else once u press save do something else...do this in ur js.U could use 2 submits instead of one submit and one button. – Standin.Wolf Commented Feb 21, 2014 at 5:26
  • You cant use a button to send send data to another page,you will have to use a submit. – Standin.Wolf Commented Feb 21, 2014 at 5:28
Add a ment  | 

3 Answers 3

Reset to default 0

You should write another javascript function and map it to the onclick event of save button

<td><input type="button" onclick="return saveForm();" value="Save as draft" /></td>

And the java script method as ,

Java script

function saveForm(){
document.form[0].submit;
}

In your servlet get the attributes and show your filled form.

Hope it helps!!

Try this

<td><input type="submit" name="submit" value="Submit" onclick="return js_file_for_validation()" autofocus="autofocus"/></td>
<td><input type="submit" name="submit1" value="Save as draft" /></td>

At servlet.java

if (request.getParameter("submit") != null) {
 //add values to the database  

} else if (request.getParameter("submit1") != null) {
      //save as draft button is clicked
}

The save operation is following as:

this servlet page:

    public class UserController extends HttpServlet 
    {
        private static final long serialVersionUID = 1L;
        private static String DEP_EDIT="/editdepartment.jsp";
    //other details
    }

    protected void dotGet(HttpServletRequest request,
      HttpServletResponse response) throws ServletException, IOException 
    {
  if(action.equalsIgnoreCase("depedit"))
    {
        forward=DEP_EDIT;
        int dep_Id=Integer.parseInt(request.getParameter("dep_Id"));
        User user=dao.getUserById(dep_Id);
        request.setAttribute("user", user);
    }  //other operation
    }

this is save.jsp page:

  <form method="post" action='UserController?action=depedit' >

            Department ID : <input type="text" name="dep_id"
                value="<c:out value="${user.dep_id}" />" /> <br />
            Employee Department: <input type="text" name="depName" 
                value="<c:out value="${user.depName}"/>"/><br/>

            <input type="submit" value="save"/>

And the submit is same as save, just look at that code and following links.

   public class UserController extends HttpServlet 
    {
        private static final long serialVersionUID = 1L;
        private static String DEP_LIST="/departmentlist.jsp";
    //other details
    }

    protected void dotGet(HttpServletRequest request, 
       HttpServletResponse response) throws ServletException, IOException 
{
if(action.equalsIgnoreCase("departmentlist"))
    {
        forward=DEP_LIST;
        request.setAttribute("users", dao.getAll());
    }
} 

this is submit.jsp page:

 <form method="post" action='UserController?action=depedit' >

            Department ID : <input type="text" name="dep_id"
                value="<c:out value="${user.dep_id}" />" /> <br />
            Employee Department: <input type="text" name="depName" 
                value="<c:out value="${user.depName}"/>"/><br/>

            <input type="submit" value="SUBMIT"/>

click this link to get the all type of submit buttons and full details

  1. https://stackoverflow./a/21927496/3242978
发布评论

评论列表(0)

  1. 暂无评论