Pankash Mann
Pankash Mann

Reputation: 59

Add CheckBox Checked values in DataBase Jquery

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

Upvotes: 0

Views: 2822

Answers (2)

Farzad Farazi A.
Farzad Farazi A.

Reputation: 26

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);                    
                }
            });
});

Upvotes: 1

Pramod
Pramod

Reputation: 434

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>

Upvotes: 0

Related Questions