Results 1 to 3 of 3

Thread: How to Save multiple checkbox values to sql database?

  1. #1
    Join Date
    May 2014
    Posts
    68

    Default How to Save multiple checkbox values to sql database?

    I have a html form, and i have also checkboxes in it. Values of checkboxes is coming from SQL-database via Web Api.

    $(document).ready(function () {
    var $MaterialName = $('#materiallist');

    function addMaterials(material) {
    $MaterialName.append('<input type="checkbox" >' + material.MaterialName + ' </input>');
    }
    <div class="form-group">
    <label for="material" class="col-xs-3 control-label header">Käytetyt materiaalit</label>
    <div class="col-xs-7">
    <div class="checkbox" id="materiallist"></div>
    </div>
    </div>
    I want to save those checked values to another SQL-database (named Form), along with the other input values in that form.

    I haven't found a solution where all the other input fields also needs to be saved as well, only solutions where is only checkboxes and button. Also, my values are coming from a database, and are not "hard-coded" options.

    I tried this:

    function getCheckedMaterials() {
    var materialArray = [];
    $("#materiallist input:checked").each(function () {
    materialArray.push($(this).val());
    });
    var selected;
    selected = materialArray.join(',');
    alert("You have selected " + selected);
    }
    This doesn't work as I need, because I can't get the values.

    All these values from other input fields goes to the Form database when I press the #tallenna button. I need the checked values to be saved in MaterialName as text.

    $('#tallenna').click(function () {
    var form = {
    FormFiller: $FormFiller.val(),
    CustomerContact: $CustomerContact.val(),
    ReadyToDate: $ReadyToDate.val(),
    Instructions: $Instructions.val(),
    Amount: $Amount.val(),
    PcsAmount: $PcsAmount.val(),
    ChargeFull: $ChargeFull.val(),
    ChargeByPcs: $ChargeByPcs.val(),
    FreightCost: $FreightCost.val(),
    CustomerName: $CustomerName.val(),
    WorkName: $WorkName.val(),
    MaterialName: getCheckedMaterials // ????
    };

  2. #2

    Default

    For checkboxes, only those you select will be sent to the server. And if you name them all like boxes[] the selected boxes will be sent as an array.

    Consider this example:
    01
    <!DOCTYPE html>
    02
    <html>
    03
    <head><title>Example</title></head>
    04
    <body>
    05
    <form action="" method="post">
    06
    <input type="checkbox" name="boxes[]" value="1">1</input>
    07
    <input type="checkbox" name="boxes[]" value="2">2</input>
    08
    <input type="checkbox" name="boxes[]" value="3">3</input>
    09
    <br><input type="submit">
    10
    </form>
    11
    <pre><?php
    12
    if(isset($_POST['boxes']))
    13
    {
    14
    foreach ($_POST['boxes'] as $_boxValue)
    15
    {
    16
    echo "Box #{$_boxValue} was selected!\n";
    17
    }
    18
    }
    19
    ?></pre>
    20
    </body>
    21
    </html>

  3. #3
    Join Date
    Jul 2019
    Posts
    264

    Default

    Each checkbox is a simple True/False value. Unless you're using a TriState checkbox that is...

    If you're using a normal checkbox, you check either use one database field for each checkbox, or you can assign bit flag values to each checkbox and "OR" those togther and save the resulting value.

Similar Threads

  1. Replies: 8
    Last Post: 05-06-2021, 02:30 PM
  2. Aspnix.com Database Vault for Windows - fast backups, compression, scheduled backups!
    By Rumsfo in forum Web Hosting and Related Offers Forum
    Replies: 0
    Last Post: 04-06-2016, 02:36 PM
  3. Multiple IP Shared Hosting | Multiple IP VPS Hosting | Multiple IP Dedicated Hosting
    By rf-harris in forum Web Hosting and Related Offers Forum
    Replies: 0
    Last Post: 04-05-2016, 03:02 PM
  4. WP/Joomla Hosting, Fast and Reliable Database Host - Prewebhost!
    By Prewebhost in forum Web Hosting and Related Offers Forum
    Replies: 0
    Last Post: 09-01-2015, 01:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •