[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 490
  • Last Modified:

Comma Separated Check boxes need to only select one

Hi, I have 4  text fileds in a form that are populated with coma seperated checkboxes from a mySqL  using database, and PHP the 4 fileds display all the checkboxes just fine, but i need to add a script that would only allow 1 checkbox to be selected out of all of them.  Can anyone help with javascript for this.

here is the code being used for the text fields.

<td width="216" valign="top" bgcolor="#FFFFFF" class="body"><table width="100%" border="0" cellpadding="2" cellspacing="0">
                          <tr class="body">
                            <td height="32" valign="middle" bgcolor="#EFEFEF" class="body"><span class="bodybold">FAA Flight Training<br>
                              Type Rating Initial </span></td>
                          </tr>
                          <tr class="body">
                            <td valign="top" class="boxlineBTM"><input name="choice_ffttri" class="body" id="choice_ffttri" size="16" wdg:recordset="faaTraining" wdg:subtype="CommaCheckboxes" wdg:type="widget" wdg:displayfield="faa_init_name" wdg:valuefield="faa_init_name" wdg:groupby="1"></td>
                          </tr>
                         
                          <tr>
                            <td bgcolor="#FFFFFF">&nbsp;</td>
                          </tr>
                          <tr class="body">
                            <td height="32" colspan="-2" valign="middle" bgcolor="#EFEFEF" class="body"><span class="bodybold">Type Rating<br>
                              Prior Experience</span></td>
                          </tr>
                          <tr class="body">
                            <td colspan="-2" valign="top" class="boxlineBTM"><input name="choice_trpe" class="body" id="choice_trpe" size="16" wdg:recordset="PriorExp" wdg:subtype="CommaCheckboxes" wdg:type="widget" wdg:displayfield="type_prior_name" wdg:valuefield="type_prior_name" wdg:groupby="1"></td>
                          </tr>
                         
                        </table></td>
                        <td width="244" colspan="-2" valign="top" class="body"><table width="101%" border="0" cellpadding="2" cellspacing="0" class="boxlineBTM">
                          <tr class="body">
                            <td height="32" colspan="-2" valign="middle" bgcolor="#EFEFEF" class="body"><span class="bodybold">Recurrent<br>
Training</span></td>
                          </tr>
                          <tr class="body">
                            <td colspan="-2" valign="top" class="boxlineBTM"><input name="choice_rt" class="body" id="choice_rt" size="16" wdg:recordset="Recurrent" wdg:subtype="CommaCheckboxes" wdg:type="widget" wdg:displayfield="recurrent_name" wdg:valuefield="recurrent_name" wdg:groupby="1"></td>
                          </tr>
                         
                          <tr>
                            <td bgcolor="#FFFFFF">&nbsp;</td>
                          </tr>
                          <tr>
                            <td bgcolor="#EFEFEF"><strong class="bodybold">Other Courses</strong></td>
                          </tr>
                          <tr>
                            <td><input name="choice_oc" class="body" id="choice_oc" wdg:recordset="othercourse" wdg:subtype="CommaCheckboxes" wdg:type="widget" wdg:displayfield="other_name" wdg:valuefield="other_name" wdg:groupby="1"></td>
                          </tr>
                        </table></td>
0
romarc
Asked:
romarc
1 Solution
 
nizsmoDeveloperCommented:
Please correct me if I am wrong, but you are wanting to allow only 1 selection out of the 4 choices? If so, why not just use radio buttons?

Apologies in advance if I misunderstood your question.
0
 
rlbalanCommented:
I believe what u need is a radio.. but if you cant change your requirement.. you might need something like this

function validateSelect() {
  //MYSELECT is the select form field Name
  var selects = getSelected( this.form[0].MYSELECT.options)
  if(selects.length > 1)
  {
    alert("Please select just one"); return false
  }
  return true;
}

  function getSelected(opt) {
      var selected = new Array();
      var index = 0;
      for (var iLoop=0; iLoop < opt.length; iLoop++) {
         if (opt[iLoop].selected) {
            index = selected.length;
            selected[index] = new Object;
            selected[index].value = opt[iLoop].value;
            selected[index].index = iLoop;
         }
      }
      return selected;
   }
0
 
romarcAuthor Commented:
Thanks rlbalan,
Im using adobe Developer Toolbox to generate the comma separated checkboxes and i don't have the option for radio btns. The code you sent looks like it will work but how can i apply this function to the 4 different text fields acting as containers for the checkboxes.  thanks
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
rlbalanCommented:
Couple of options,
if you want this alert to trigger when the chekbos is selected, you need to add "onChange" event to each of these checkboxes. I havent done actionscripts, but I assume there is some way to add the onChange event to these checkboxes
onChange="javascript:validateSelect()"

Else, u can call this function when you click the submit button of that page.
onClick="javascript:validateSelect()"
0
 
romarcAuthor Commented:
thanks, it would be great if when i selected one of the dynamci check boxes it de-selects the other if their is another selected. would eleiminate an error.
0
 
basicinstinctCommented:
if you only ever want one selected then you should be using radio buttons instead of checkboxes
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now