0

How to save Checkbox Checked values in Database

Below is my code

<input type="text"  value="" name="Productname" />

<input type="checkbox" name="Product" value="0.5">
<input type="checkbox" name="Product" value="0.07">
<input type="checkbox" name="Product" value="0.5">
<input type="checkbox" name="Product" value="0.63">
<input type="checkbox" name="Product" value="0.63">
<input type="checkbox" name="Product" value="0.5">

 <input type="submit" id="btn1" value="Add" class="btn btn-primary" />

Here on submit click I want to save Checked values under One Productname and want to send as an object to controller

2 Answers 2

1

At first set id for each checkbox. When click on #btn1, read all checkboxes and if checked, store checkbox's id in selected[] array. Then send it by jquery ajax to target page.

    $("#btn1").click(function () {

            var selected = new Array();
            var i = 0;
            $(':checkbox').each(function () {
                var checked_status = this.checked;
                if (checked_status == true) {
                    selected[i] = $(this).attr("id");
                    i++;
                }
            });


            $.ajax({
                url: "Ajax.aspx", //Target page address
                type: "GET",
                async: true,
                cache: false,

                data: { s: selected },

                success: function (text) {
                    alert("successfully:" + text);
                    $("input:checkbox").each(function () {
                        if ($(this).attr("checked")) {
                            $(this).prop('checked', false);
                        }
                    });
                },
                error: function (xhr, status, error) {
                    alert("Error: " + status + error);                    
                }
            });
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for providing me answer but instead of id of checkbox I used val to get value of checkbox ,and when i console the list i get the selected value but last value is "on". What it can be ?
0

Try below code for get all checkbox value.

<form  method="POST">
<input type="text"  value="" name="Productname" />

<input type="checkbox" name="Product" value="0.5" id="myCheck">
<input type="checkbox" name="Product" value="0.07">
<input type="checkbox" name="Product" value="0.5">
<input type="checkbox" name="Product" value="0.63">
<input type="checkbox" name="Product" value="0.63">
<input type="checkbox" name="Product" value="0.5">

 <input type="button" id="btn1" value="Add" class="btn btn-primary" onclick="myFunction(this.form)"/>
 </form>

<script>
function myFunction(frm) {
var values = "";

    for (var i = 0; i < frm.Product.length; i++)
    {

        if (frm.Product[i].checked)
        {

                values = values+frm.Product[i].value + ",";

        }
    }
    alert(values);
}
</script>

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.