How can I pass a parameter via submit button?
In my code for an mini online book store i have a following line repeating 5 times with different value for 'name' parameter
<input name="JSP-2" type="submit" value="Buy">
On clicking the button Buy, t开发者_C百科he application redirects to a file buy.jsp where it gets the value of name and displays corresponding details of the book.
In my buy.jsp, I have included
<% String bname= request.getParameter("name");
out.print(bname);
%>
But the name doesnt get assigned to bname and it shows the value as null. How do I pass a parameter from the submit type input? Please help.
You have to pass the parameter in the request. Since you are having a form, and submitting the form, you can have a hidden field in the form called, say "submitType", and populate it whenever you click the button, using javascript. Then this will be available in the next request.
Somewhere inside the form :
<input type="hidden" name="submitType">
in the submit buttons:
<input name="JSP-2" type="submit" onclick="setType('Buy')">
Javascript: formName is the name of your form
<script>
function setType(type)
{
//formName is the name of your form, submitType is the name of the submit button.
document.forms["formName"].elements["submitType"].value = type;
//Alternately, you can access the button by its Id
document.getElementById("submitId").value = type;
}
</script>
精彩评论