0

Im trying to make a simple form with radio buttons in a JSP-page. After getting it to work, I would like to send the user to a new page called "matematikQuiz", but right now I am just focusing on letting the java know which of the three radio buttons has been pressed. Right now the way I see it, if the user chooses Female and submits it with the submit button, an alert should pop up. But it seems like the getParameter() method isn't working with the radiobuttons. It works fine with pressing the submit button though, but I was told it could check radio buttons as well, given the "name" (gender in my case). Any help would be much appreciated. :-)

<form action="matematikQuiz.jsp" method="post">
    <input type="radio" name="gender" value="male"> Male<br>
    <input type="radio" name="gender" value="female"> Female<br>
    <input type="radio" name="gender" value="other"> Other 
</form>

<form action="matematikQuiz.jsp" method="post">
    <input type="submit" name="sendAnswer" value="Send the answer"></input>
</form>
<%
    if (request.getParameter("sendAnswer") != null) {
        //button has been pressed.

        String yourChoice = request.getParameter("gender");
        if ("male".equals("gender")){
            //you have selected male.
        } else if ("other".equals("gender")){
            //you have selected female.
          } else if ("female".equals("gender")){ 
%><script>
alert("you have selected <%=yourChoice%>");
</script>
<%
3
  • have you tried my answer code? Commented May 13, 2016 at 14:06
  • Also if ("male".equals(yourChoice)) {. Commented May 13, 2016 at 14:18
  • Accept my answer if it fix your issue. Commented May 13, 2016 at 14:47

1 Answer 1

1

create single form instead of two, because second form don't have radio buttons, and your radio present in first form,

keep submit button in first form, try below code

<form action="matematikQuiz.jsp" method="post">
    <input type="radio" name="gender" value="male"> Male<br>
    <input type="radio" name="gender" value="female"> Female<br>
    <input type="radio" name="gender" value="other"> Other <br>

 <input type="submit" name="sendAnswer" value="Send the answer"></input>
</form>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.