1

I am displaying a modal after a button click . The code of button for opening a modal is as follows :

 <button type="button" name="btnEditIP" data-toggle="modal" data-target="#myModal" class="sultan_btn">Edit IP</button>\

The code of modal is as follows :

<div id="myModal" class="modal fade">
     <div class="modal-dialog">
        <div class="modal-content">
            <form id="ipForm" class="form-horizontal" role="form" method="post" action="info_edit.jsp">
                  <div class="modal-header modal-header-warning">
                      <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                          <h4 class="modal-title ">Please Enter IP Address</h4>
                  </div>

                  <div class="modal-body"> 
                     <div class="form-group">
                          <label for="inputName">IP Address</label>
                          <input type="text" class="form-control" name="ip"/>
                 </div> 

                 </div>
                 <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                    <button type="button" name="btnSave" id="btnSave" class="btn btn-primary" data-dismiss="modal">Save</button>
                 </div>
            </form>
        </div>
   </div>

In edit_info.jsp page, I am trying to determine whether the btnSave button is pressed by the following code :

if ((request.getParameter("btnSave") == null) ? false : true) {
      out.println("Form submitted");
}
else{
     out.println("Form not submitted");
}

Although , I am pressing the Save button in modal , I am always getting this message , "Form not submitted!" .That means , either the form is not submitted or there is an error in pressing button .

I am not sure where is the error . I have tried a lot but could not identified where is the error . Please help me to solve this error .

Christopher Marlowe
  • 2,098
  • 6
  • 38
  • 68
  • the quick fix is setting form action to `info_edit.jsp?btnSave=true` in your `
    ` element. In general you need an ` with `name = btnSave` for this to work. Button name will not be transmitted as a query parameter which you are fetching using `.getAttribute("btnSave")` as far as I know.
    – trk Nov 29 '17 at 05:16
  • What is about getParameter("btnSave") ? I am used to use this function . – Christopher Marlowe Nov 29 '17 at 05:23
  • Why do you need to check "btnSave", It won't go to edit_info.jsp untill and unless save btn is clicked. – sujit tiwari Nov 29 '17 at 05:29
  • Beacasuse the code of form is also in the "edit_info.jsp" page . – Christopher Marlowe Nov 29 '17 at 05:31

1 Answers1

1

Please change request.getAttribute("btnSave") to request.getParameter("btnSave").

Because getParameter() returns http request parameters.

EDIT

I am not sure if one would see btnSave server side since the button name would not be sent ?

We can use type="submit" to send the pressed button's name to the server.

<input type="submit" name="btnSave" value="SAVE"/>

Also Check:

Difference between getAttribute() and getParameter()

Difference between type='button' and type='submit'

How do I call a specific Java method on click/submit event of specific button in JSP?

Gurkan Yesilyurt
  • 2,635
  • 2
  • 20
  • 21