m just beginning with servlets and having a confusion.
In an html form, if there are multiple submit buttons in the same form and these requires different actions to be performed then how in the corresponding servlet we'll be able to recognize which button pressed.
thanks in advance.
<input type="submit" value="This is a submit button" name="submit1">
<input type="submit" value="Another submit button" name="submit2">
<input type="submit" value="Yet another submit button!" name="submit3">
In particular I have two buttons in the form:
<input type="submit" action="altaPerfilServlet" class="botonCrear" name="botonEnviar" id="botonEnviar" value="Crear perfil" onclick="validarEnviar()"
<%= request.getAttribute("esValido") != null && ((Boolean)request.getAttribute("esValido")) ? "" : "disabled" %> />
<input type=submit class="botonValidar" name="botonValidar "id="botonValidar" value="Validar datos" onclick="validarNickMail()" />
In my servlet at the beginning of the doGet method i wrote:
if(request.getParameter("botonVerificar") != null)
System.out.println("botonVerificar was not pressed");
System.out.println("botonVerificar was pressed");
No matter what button i press the result is: botonVerificar was pressed.
Any ideas greenhorns?
Bear Bibeault wrote:Have you fixed the incorrect code? If so, you need to show it. Please UseCodeTags.
The correct code is:
Bear Bibeault wrote:Have you used any browser tools to see if the request has the expected parameters? (And please properly indent your code -- hard to read with no indentation.)
Deleted all the useless information(like the scriptlet), now the code is easy to read.
No, I have not used any browser tools and I have no idea on how to do that.
Anyway, my problem now is different...
Now the form has: method='post', enctype='multipart/form-data'.
The method i was using to recognize which of the submit buttons was clicked (in the servlet) was iterating over all the FileItem(s) sent with the request and if the name of any of them was the name of any of the 3 buttons in my form, then that button was the one that the user clicked. This worked perfectly when the buttons had type=submit, but now that they are simple buttons apparently they are not sent with the request, so when i get the FileItems in the servlet none of them is any of the buttons... so the trick i was using before is useless now.
Jhon Salchichon wrote:No, I have not used any browser tools and I have no idea on how to do that.
so i had to modify the buttons and set type=button(they are not submit buttons anymore because with type=submit they would call the servlet always, even if the validation was false).
That's using a sledgehammer to crack walnuts. Simply return false from a submit hander to prevent the form from being submitted.
This worked perfectly when the buttons had type=submit,
Then that will start working again when you remove the logic to change the button types.