[RESOLVED]input checkboxes–

Hello,

I have a categories page with different User-Friendly divs, but a simple jquery to make each div work like a checkbox..

If users select more than one checkboxes, how do I send all request to a table in the database?

I checked similar topics, i found out there was a select name before options values… but these are divs with different values from 1 to 10…how do I get more than one value and post into the db?

you can use the "text" property of divs.

how?

<span class="button-checkbox">
        <button type="button" class="btn btn-lg" data-color="primary">
      <img src="~/img/v/science.png" alt="X" class="pull-left"><h2 class="pull-right">Science</h2>
        </button>
        <input type="checkbox" class="hidden" />
    </span>
<span class="button-checkbox"> <button type="button" class="btn btn-lg" data-color="info"> <img src="~/img/v/media.png" alt="X" class="pull-left"><h2 class="pull-right">Social Media</h2> </button> <input type="checkbox" class="hidden" /> </span>

thats how each div is divided..

If you are using jQuery to handle this, you’ll want to ensure that your actual <input> checkbox elements have specific values that you can use and access when your actual <form> is posted (either through jQuery or using a traditional form submit).
You can do this by setting a name attribute on your hidden elements so that you can identify them in your Request :

<span class="button-checkbox">
        <button type="button" class="btn btn-lg" data-color="primary">
      <img src="~/img/v/science.png" alt="X" class="pull-left"><h2 class="pull-right">Science</h2>
        </button>
        <input name='Science' type="checkbox" class="hidden" />
</span>

<span class="button-checkbox">
        <button type="button" class="btn btn-lg" data-color="info">
      <img src="~/img/v/media.png" alt="X" class="pull-left"><h2 class="pull-right">Social Media</h2>
        </button>
        <input name='SocialMedia' type="checkbox" class="hidden" />
</span>

Then assuming you are checking these hidden elements through jQuery when your button is clicked, you should then be able to check which values are checked when your form is posted from the Request :

// If the form was posted
if(IsPost)
{
     // Check your values (if the name attribute is present, the box was checked)
     bool science = Request["Science"] != null;
}

The jquery was just to beautify the divs… nothing more, the values would be requested using traditional form submit.. My headache is, am having twelve (12) input checkboxes with different values… if more than one checkbo is clicked, how do I get this
values and post them into the database.

<span class="button-checkbox">
        <button type="button" class="btn btn-lg" data-color="primary">
      <img src="~/img/v/science.png" alt="X" class="pull-left"><h2 class="pull-right">Science</h2>
        </button>
        <input name='Science' type="checkbox" class="hidden" value="1" checked="@(category == 2)">
</span>
<span class="button-checkbox">
<button type="button" class="btn btn-lg" data-color="info"> <img src="~/img/v/media.png" alt="X" class="pull-left"><h2 class="pull-right">Social Media</h2> </button> <input name='SocialMedia' type="checkbox" class="hidden" value="2" checked="@(category == 2)"> </span>

If (IsPost)

{

bool science =
Request["Science"]
!= null;

bool socialmedia=
Request["socialmedia"]
!= null;

var db = Database.Open("categories);

var sql= INSERT INTO Table_A(UserID, CategoriesID) VALUES ( @0, @1)";

db.Execute(sql,UserID,*);

}

This is what I need to fix, if there are more than one checked checkbox, how do I ensure its properly incremented in the table in the database..

I’m assuming that for each of your specific categories, you would likely want to add a checkbox for each that has a name and a specific value (that corresponds in your database to that particular category) which you are currently doing :

<input name='Science' type="checkbox" class="hidden" value="1" checked="@(category == 2)">

So then when your form is posted, you would want to check for any of your elements are checked and build a collection of these values and add them individually where present :

// Add each of your values into a List of they exist
List<string> selectedCategories = new List<string>();

// Check if each one exists and if so add it
if(Request["Science"] != null) { selectedCategories.Add(Request["Science"]); }
if(Request["socialmedia"] != null) { selectedCategories.Add(Request["socialmedia"]); }

// Open your database
var db = Database.Open("categories");

// For each value in your collection, add it
foreach(var category in selectedCategories)
{
    db.Execute("INSERT INTO Table_A(UserID, CategoriesID) VALUES (@0, @1)",UserID,category); 
}

If you are going to be expecting integer values, just ensure your convert the value prior to adding it :

db.Execute("INSERT INTO Table_A(UserID, CategoriesID) VALUES (@0, @1)",UserID, Convert.ToInt32(category)); 

Leave a Reply