Jump to content
IPV

Js: Checkboxes For Multi-Optioned Entries

Recommended Posts

Dear All,

Would you please advice for next:

Is it possible to collect Data with next checkboxes and write them into one field in the table?

 

I do believe that some special JavaScript needed here.

Many thanks in advance.

post-19905-0-79957800-1454312698_thumb.p

post-19905-0-93001500-1454312757_thumb.p

Share this post


Link to post
Share on other sites

Hello IPV,

 

Welcome to Caspio forum!

 

If I understand correctly, you have one field that stores values.

So, you can

  1. Select the Hidden FormElement for the field that stores values.
  2. Add a Virtual field, select the "Checkbox" element and enter your values, for example, "Math", to "Display Text" and "Value when checked" fields. Add as many virtual fields as you want.
  3. Add Header&Footer element, select the Footer element, click the Source button and enter the following script:
    (please enter the name of your field that stores values instead of "text"; enter the numbers of first and last Virtual fields in the group of checkboxes, in my case the first one is VirtualField1 and the last one is VirtualField3)
    <SCRIPT LANGUAGE="JavaScript">
    function concatenate()
    {
    var Resultfieldname = "text";
    var firstVirtual = 1;
    var lastVirtual = 3;
    var ResultString = "";
    var virtualFieldName = "";
    
    for (i=firstVirtual ;i<=lastVirtual; i++)
    {
    virtualFieldName = "cbParamVirtual"+i;
    if (document.getElementById(virtualFieldName).checked) ResultString = ResultString + "," + document.getElementById(virtualFieldName).value;
    }
    Resultfieldname = "InsertRecord"+Resultfieldname;
    if (ResultString.length>0) ResultString = ResultString.substr(1);
    document.getElementById(Resultfieldname ).value = ResultString;
    }
    document.getElementById("caspioform").onsubmit=concatenate;
    </SCRIPT>

    If you have more than one block, you can add blocks like:
    (please enter the name of your second field that stores values instead of "new"; enter the numbers of first and last Virtual fields in the second group of checkboxes, in my case the first one is VirtualField4 and the last one is VirtualField6)

    Resultfieldname = "new";
    firstVirtual = 4;
    lastVirtual = 6;
    ResultString = "";
    virtualFieldName = "";
    
    for (i=firstVirtual ;i<=lastVirtual; i++)
    {
    virtualFieldName = "cbParamVirtual"+i;
    if (document.getElementById(virtualFieldName).checked) ResultString = ResultString + "," + document.getElementById(virtualFieldName).value;
    }
    Resultfieldname = "InsertRecord"+Resultfieldname;
    if (ResultString.length>0) ResultString = ResultString.substr(1);
    document.getElementById(Resultfieldname ).value = ResultString;

    Then the whole script will be like:

    <SCRIPT LANGUAGE="JavaScript">
    function concatenate()
    {
    var Resultfieldname = "text";
    var firstVirtual = 1;
    var lastVirtual = 3;
    var ResultString = "";
    var virtualFieldName = "";
    
    for (i=firstVirtual ;i<=lastVirtual; i++)
    {
    virtualFieldName = "cbParamVirtual"+i;
    if (document.getElementById(virtualFieldName).checked) ResultString = ResultString + "," + document.getElementById(virtualFieldName).value;
    }
    Resultfieldname = "InsertRecord"+Resultfieldname;
    if (ResultString.length>0) ResultString = ResultString.substr(1);
    document.getElementById(Resultfieldname ).value = ResultString;
    
    //second block
    
    Resultfieldname = "new";
    firstVirtual = 4;
    lastVirtual = 6;
    ResultString = "";
    virtualFieldName = "";
    
    for (i=firstVirtual ;i<=lastVirtual; i++)
    {
    virtualFieldName = "cbParamVirtual"+i;
    if (document.getElementById(virtualFieldName).checked) ResultString = ResultString + "," + document.getElementById(virtualFieldName).value;
    }
    Resultfieldname = "InsertRecord"+Resultfieldname;
    if (ResultString.length>0) ResultString = ResultString.substr(1);
    document.getElementById(Resultfieldname ).value = ResultString;
    }
    document.getElementById("caspioform").onsubmit=concatenate;
    </SCRIPT>

I hope, it helps.

Please feel free to ask if anything is unclear.

Share this post


Link to post
Share on other sites

I need this function for one of my datapages but the difference is that I have an update form which needs to store the separate checkbox fields so I cannot use virtual fields. Can this script be used with actual checkbox fields rather than virtual fields?

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...