0

I 'm developing a form with ajax and JSP, but I'm stuck at this checkbox values. I want to send the selected checkbox values to another page.

What I'm trying to do is to get checkbox values from "app.jsp" to "senDbase.jsp".

Here I'm not using FORM.

app.jsp

<input type="checkbox" name="chb" id="chb1" value="html" />HTML<br/>
<input type="checkbox" name="chb" id="chb2" value="css" />CSS<br/>
<input type="checkbox" name="chb" id="chb3" value="javascript" />JavaScript<br/>
<input type="checkbox" name="chb" id="chb4" value="php" />php<br/>
<input type="checkbox" name="chb" id="chb5" value="python" />Python<br/>
<input type="checkbox" name="chb" id="chb6" value="net" />Net<br/>
<input type="button" value="Click" id="btntest" />

Here is what I've tried

try
{
    var url="senDbase.jsp";
    url += "&chkbx1=" +document.getElementById("chb1").value;
    url += "&chkbx2=" +document.getElementById("chb2").value;
    url += "&chkbx3=" +document.getElementById("chb3").value;
    url += "&chkbx4=" +document.getElementById("chb4").value;
    url += "&chkbx5=" +document.getElementById("chb5").value;
    url += "&chkbx6=" +document.getElementById("chb6").value;
}
catch(e)
{
    alert('error sending variables');
}

I'm not getting these values on the "senDbase.jsp".

Community
  • 1
  • 1

2 Answers2

0

You can use javascript/jquery. For example, each time the checkbox is check or uncheck an ajax call is going to be done. I'm assuming you don't want to do a redirect.

  var checked;
 $(document).on("change", "input[type=checkbox]", function(e) {
    checked = $("input[type=checkbox]:checked");
    if ($(this).is(':checked')) {
            var id=$checked.val();
            $.ajax({
                url: 'senDbase.jsp',
                type: 'GET',
                data: "&chkbx1="+id
            });
        }
 });
Diego
  • 916
  • 1
  • 13
  • 36
  • @ Diego, you are right, I don't want redirect, also I'm not using form, since its a pretty big application. Can you write the code purely in Ajax not Jquery, please. Thanks for responding. – JavaLearner Sep 13 '14 at 05:25
0

I assume you are missing the ? character in your URL since you cannot retrieve the query parameters on the senDbase.jsp:

try
{
    var url="senDbase.jsp?";
    url += "&chkbx1=" +document.getElementById("chb1").value;
    url += "&chkbx2=" +document.getElementById("chb2").value;
    url += "&chkbx3=" +document.getElementById("chb3").value;
    url += "&chkbx4=" +document.getElementById("chb4").value;
    url += "&chkbx5=" +document.getElementById("chb5").value;
    url += "&chkbx6=" +document.getElementById("chb6").value;
}
catch(e)
{
    alert('error sending variables');
}
tmarwen
  • 15,750
  • 5
  • 43
  • 62
  • @ Tmarwen, Thanks for the response. Since its a very big application I haven't wrote the whole code in here. I'm sure the Ajax code has the '?' at the beginning of the code, this is just a part of it. Thank you once again. – JavaLearner Sep 13 '14 at 05:28