Let say I have a simple form with no required fields:
<form action="index.jsp" method="post">
<input type="text" name="firstName" />
<input type="text" name="lastName" />
<input type="text" name="email" />
<input type="submit" value="submit" />
</form>
I want to check if the form was submitted by checking the submit
parameter (because it's always present). In PHP
I can do a simple
if ( $_POST['submit'] )
but the request.getParameter("submit")
doesn't seem to work.
So what's the best way to check if a form was submitted?
You need to give the input element a name. It's the element's name which get sent as request parameter name.
<input type="submit" name="submit" value="submit" />
Then you can check it as follows:
if (request.getParameter("submit") != null) {
// ...
}
You perhaps also want to check if "POST".equalsIgnoreCase(request.getMethod())
is also true.
if ("POST".equalsIgnoreCase(request.getMethod()) && request.getParameter("submit") != null) {
// ...
}
Better, however, would be to use a servlet and do the job in doPost()
method.
You can try this way:-
if ("POST".equalsIgnoreCase(request.getMethod())) {
// Form was submitted.
} else {
// It may be a GET request.
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With