0

I have values of checkbox and as soon as I click delete the values and the type must be sent to a servlet called DeleteServlet. Please help me how to send the values?

<html>
    <head>
    <script type="text/javascript">
     function get_check_value(chkbox){
           if(chkbox.checked){
          var Values=chkbox.value;
          //send this Values to servlet

             }

    }
    function doDelete(theClick){
    var type=theClick;
//send this type to servlet
    alert(type);

    }

    </script>
    </head>
    <%String user=request.getParameter("Users"); %>
    <body>
    <%

    String User[]=user.trim().split(" ");

    for(int i=0;i<User.length;i++){%>
     <center>
        <form name='list' action='AddUserServlet'>

     <%  out.println("<input type='checkbox' name='user" + i + "' value='"+ User[i] + "' onClick='return get_check_value(this)'>" + User[i]+ "</input>" ); %>  

    </form> 

    <%}%>

        <input type="submit" value="ADD" name="b1" >
        <input type="submit" value="MODIFY" name="b1">
     <input type="submit" value="DELETE" name="b1" onClick="return doDelete(value)" > 

      </center>

    </body>
    </html>

I tried this "document.list.user.value" in an alert box it comes as undefined object. To send to a servlet I tried the following line but nothing is read

chk_val=chkbox.value;
document.add.action = "AddUserServlet?type=delete&name="+chk_val;
 document.add.submit();
0

2 Answers 2

2

You don't need Javascript for this at all.

Just change your code so that the HTML form is generated as follows:

<form action="AddUserServlet" method="post">
    <input type="checkbox" name="user" value="1">
    <input type="checkbox" name="user" value="2">
    <input type="checkbox" name="user" value="3">
    <input type="checkbox" name="user" value="4">
    <input type="checkbox" name="user" value="5">

    <input type="submit" name="b1" value="ADD">
    <input type="submit" name="b1" value="MODIFY">
    <input type="submit" name="b1" value="DELETE">
</form>

(the checkbox value must be the unique user ID)

Then use request.getParameterValues() in your AddUserServlet:

String b1 = request.getParameter("b1");
if ("DELETE".equals(b1)) {
    String[] users = request.getParameterValues("user");
    // ...
}

The users array will contain the checked values (1, 2, 3, 4 and/or 5).

Sign up to request clarification or add additional context in comments.

2 Comments

Ya I can do like this but the problem is I get the values from database. I must use for loop here to get values. Will I be able to change the code so?
Certainly. Just replace name='user" + i + "' by name='user'. Use your logical thinking powers :)
1

Could you use jQuery javascript libary. It is easy to use and you will find lots of tutorials. Here is good tutorial about get method(AJAX) http://api.jquery.com/jQuery.get/

Is it possible to do it without javascript?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.