You are viewing limited content. For full access, please sign in.

Question

Question

How to populate Checkbox from a database

asked on September 8, 2014

I have a form that collects multiple 5 option checkbox questions, using a workflow it populates a fillable PDF and then stores that information into a SQL Data base.  We need to be able to pull that info from the database into a second form for updating/editing.  Is there any way to populate any form of multiple choice option using the data that started as a form checkbox and stored/retrieved from a SQL database using a stored procedure?

 

0 0

Answer

SELECTED ANSWER
replied on October 11, 2014

Okay so this this is what I have put together and seems to work in my small test environment.  I will be adapting it to the full evaluation form over the course of the next couple days.  Uses the CSS types of filledfield and dropdown as listed by Kenneth above and uses a change field trigger on the employee ID field that is filled by the initial stored procedure so that all the other single line "filledfield"s have values when the actual radio button/checkboxes are populated.  I have Yes-No, A-B-C, A-B-C-D-E, and A-B-C-D-E-F-G questions through out the form that have the "dropdown".  I hope this helps others that need this type of solution.  I have chosen to use radio buttons to keep from having extra code to limit the number of checkboxes that can be clicked at a time.  We are looking for a single response from these questions that allow us to change values as needed with out having the client choose every question over to update the database.

 

$(function() {
  $(document).ready(function() {
     var trigField = document.getElementById("Field10");
     trigField.onchange = function () {
       fill7choiceRadio("recommendation","Field182")
       fill3choiceRadio("gpAction","Field183")
       fill5choiceRadio("v_S01G01","Field137");
       fill5choiceRadio("v_S01G02","Field149");
       fill5choiceRadio("v_S02G01","Field187");
       fill5choiceRadio("v_S03G01","Field153");
       fill5choiceRadio("v_S03G02","Field154");
       fill5choiceRadio("v_S03G03","Field155");
       fill5choiceRadio("v_S04G01","Field156");
       fill5choiceRadio("v_S04G02","Field157");
       fill5choiceRadio("v_S05G01","Field158");
       fill5choiceRadio("v_S05G02","Field159");
       fill2choiceRadio("S01G02cd","Field151");
       fill2choiceRadio("gpPlanWorked","Field181")
       fill2choiceRadio("S06a","Field170");
       fill2choiceRadio("S06b","Field171"); 
       fill2choiceRadio("S06c","Field172");
       fill2choiceRadio("S06d","Field173");
       fill2choiceRadio("S06e","Field174");
       fill2choiceRadio("S06f","Field175");
       fill2choiceRadio("S06g","Field176");
       fill2choiceRadio("S06h","Field177");
       fill2choiceRadio("S06i","Field178");
    };
  });
});
// Yes-No RadioButton
function fill2choiceRadio (inp, targ) {
	var value = $('.' + inp + ' input').val();
  switch(value) {
          case "1": 
              document.getElementById(targ + '-0').checked = true;
              break;
          case "2":
              document.getElementById(targ + '-1').checked = true;
              break;
          default:
            alert(value);	
  }
}
// A-B-C RadioButton
function fill3choiceRadio (inp, targ) {
  var value = $('.' + inp + ' input').val();
  switch(value) {
          case "1": 
              document.getElementById(targ + '-0').checked = true;
              break;
          case "2":
              document.getElementById(targ + '-1').checked = true;
              break;
          case "3": 
              document.getElementById(targ + '-2').checked = true;
              break;
          default:
            alert(value); 
  }
}
// A-B-C-D-E RadioButton
function fill5choiceRadio (inp,targ) {
   var value = $('.' + inp + ' input').val();
    switch (value){
      case "1": 
          document.getElementById(targ + '-0').checked = true;
          break;
      case "2":
          document.getElementById(targ + '-1').checked = true;
          break;
      case "3": 
          document.getElementById(targ + '-2').checked = true;
          break;
      case "4": 
          document.getElementById(targ + '-3').checked = true;
          break;
      case "5": 
          document.getElementById(targ + '-4').checked = true;
          break;
      default:
        alert(value);
    }
}
// A-B-C-D-E-F-G RadioButton
function fill7choiceRadio (inp,targ) {
   var value = $('.' + inp + ' input').val();
    switch (value){
      case "1": 
          document.getElementById(targ + '-0').checked = true;
          break;
      case "2":
          document.getElementById(targ + '-1').checked = true;
          break;
      case "3": 
          document.getElementById(targ + '-2').checked = true;
          break;
      case "4": 
          document.getElementById(targ + '-3').checked = true;
          break;
      case "5": 
          document.getElementById(targ + '-4').checked = true;
          break;
      case "6": 
          document.getElementById(targ + '-5').checked = true;
          break;
      case "7": 
          document.getElementById(targ + '-6').checked = true;
          break;
      default:
        alert(value);
    }
}

 

0 0
replied on October 11, 2014

Can you explain to us why the need for the multiple almost identical functions? I would use the last one for all of them. It should always work without a problem. If the field is only able to be 1 of two options, then it doesnt need to worry about the next item in the case anyways. 

0 0

Replies

replied on September 9, 2014

Only way I know of is to have a hidden field that gets filled in by the lookup, then have Javascript to have that value relate to which checkbox to mark. 

1 0
replied on September 9, 2014

 

I was able to get this working as a test.

 

If you need multiple checkboxes to be able to be selected, I would recommend filling the hidden field with all the values, separated by some unique value. Then you can use Javascript to iterate through the values using a split function on the string and you will be able to select all your options. 

 

$(function() {
  $(document).ready(function () {
    var value = $('.FilledField input').val();
    switch (value){
      case "1": 
          document.getElementById('Field2-0').checked = true;
          break;
      case "2":
          document.getElementById('Field2-1').checked = true;
          break;
      case "3": 
          document.getElementById('Field2-2').checked = true;
          break;
      default:
        alert(value);
    }
  });
});

If you change the "true" to "false" it will unselect that checkbox for you. 

0 0
replied on September 9, 2014 Show version history

I have no experience with Java so hope you don't mind the probably simple question.    What would multiple statements look like? 

 

Thank you in advance.

0 0
replied on September 9, 2014

I actually realized I over complicated things, which happens a lot when you deal with stuff like this. You wouldn't need to format the text, just go ahead and loop through all the auto-suggestions, similar to how it is done here:

$(function() {
  $(document).ready(function () {
     var tester = $('.FilledField datalist select option').length;
     if (tester > 0) {
        $('.FilledField datalist option').each(function(){
            var option = $(this).val() - 1;
            document.getElementById('Field2-'+option).checked = true;
        });
     }
  });
});
$(function() {
  $(document).ready(function () {
     var tester = $('.FilledField datalist select option').length;
     if (tester > 0) {
        $('.FilledField datalist option').each(function(){
            var option = $(this).val() - 1;
            document.getElementById('Field2-'+option).checked = true;
        });
     }
  });
});

I subtracted '1' from the values in the suggestion to be able to adjust for how Javascript references the field. You can remove it if you start at zero for the suggestions. 

 

I put in the suggestions "1" and "3" and got this output:

 

You will need to adjust the number in  "Field2-" part to be whatever field it is you are using.  Also, you will need this to be a function since it will need to trigger after the lookup is performed. Can you post some sort of screenshot or inform me the field number/CSS Class of the field that would cause the lookup to occur? 

 

Also, if you tell me what field number your checkbox is, I can help iwth that as well. You will need to apply a CSS class like I did to the hidden field for it to find and go through all the values

 

 

 

0 0
replied on September 11, 2014

I think I may have misunderstood the request here.

 

If I am correct, you want to take a checkbox field and use it's selected options in a SQL Query/lookup so that in the next form step that certain information is populated from that information to other fields? 

 

To do this, we need to understand how each option effects the query. Say, if you want it to be if the checkbox is enabled for option 1, then 1 lookup occurs, but for each subsequent checkbox that is selected, a second lookup is performed that updates other fields. 

 

The other option is each option selected effects the overall lookup and you set up only one lookup to occur.

 

Either way, we will need the use of a hidden field (or 5 if you choose the first option I mentioned) and use javascript to take the selected items and fill the field(s) with information depending on if they are selected. Then, in the second form, we would use javascript to trigger the lookup when the form is loaded, giving you the result you wanted.

 

Let me know which functionality you are looking for so that I can give you a hand in getting started.

0 0
replied on September 11, 2014 Show version history

Kenneth,  I am looking to take a value from a stored procedure into a single text box  and then use the content of that text box (Value 1-5 but a single value) to select a check box in the form.  I have tried to get your first example to work as seen in the attached photo but am failing miserably.  Did I mention I have no understanding of Java and the data guy in me says it's just another variable why is this functionality not built into the system...

 

So if you can point out where I have messed up your code it would be great.

 

 

fillable boxes.PNG
0 0
replied on September 11, 2014 Show version history

You changed things to 'q' Change all those to 'Field'

 

Also, in line 3, you need to keep it how I recommended in the field place:

var value = $('.FilledField input').val();

Otherwise it is not getting the value from the field. 

 

Last thing, you will need a value in that field, so set a default value for it as a test, or add a field that would represent the field that causes the lookup, at which point, you will want to assign that field a CSS Class name like "TriggerField" and then you would need to change the code to the below code snippet:

 

$(function() {
  $(document).ready( function() {
     function Checked() {
      var value = $('.FilledField input').val();

      document.getElementById('Field2-0').checked = false;
      document.getElementById('Field2-1').checked = false;
      document.getElementById('Field2-2').checked = false;
      document.getElementById('Field2-3').checked = false;
      document.getElementById('Field2-4').checked = false;

      value -= 1;
      document.getElementById('Field2-'+value).checked = true;
    }

    $('.TriggerField').on('change','input',Checked);
  });
});

 

 

0 0
replied on October 10, 2014

Okay so now I have this working in a controlled test form, but when I try to populate the filled fields with a view based off an the entry of an employee number it does nothing.  Is there a way to postpone the execution of the java until after a field is entered?

0 0
replied on October 10, 2014

try adding the following right under the line that starts with  "function"

 

alert('test');

If this runs then at least we know that the code is starting but it may not be working properly. If it doesnt start, you may have configured things wrong and should review what i have recommended in my previous posts.

1 0
replied on October 11, 2014

It runs I did that at the first couple steps, It helped me figure out that the function was running before the user had an opportunity to enter a value in the search for box.

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.