Menu

Number of options for check boxes

Help
astro
2007-03-16
2013-06-03
  • astro

    astro - 2007-03-16

    In my form I have sets of check boxes (or radio buttons if needed) that need more then 1 option to be able to be selected but only up to a certain number (ie, they must select 1 but can select up to 2 or 3 or 100000000 well not really that many but u know the thing) Just wondering how to do this

     
    • TNTEverett

      TNTEverett - 2007-03-16

      Open the process.php file.  Look for the last line starting with "pt_register('".  After this line add some code that looks like this;

      $group1cnt=0;
      if ($checkbutton1){$group1cnt++}
      if ($checkbutton2){$group1cnt++}
      if ($checkbutton3){$group1cnt++}
      if ($checkbutton4){$group1cnt++}
      if ($group1cnt == 0){$errors=1; $error.="Group1: You Must select at least one.!";}
      if ($group1cnt > 2){$errors=1; $error.="Group1: You Must select at least one, and no more than 2.!";}

      This won't be your exact code but you can see the basic idea. 
      1.) Set a variable to count the check boxes.
      2.) Count the number of checked options.
      3.) Display an error message if the count is lower or higher than expected. 

       
    • ricx

      ricx - 2007-04-09

      I have the same issue with the checkboxes. I tried what you said above but I keep getting a parse error that the IF statement is at the wrong line. Below is what I have on the process.php - so where would I put the variable count and if statements?

      <?php
      include("global.inc.php");
      $errors=0;
      $error="The following errors occured while processing your form input.<ul>";
      pt_register('POST','Name');
      pt_register('POST','Address1');
      pt_register('POST','Address2');
      pt_register('POST','State');
      pt_register('POST','Zipcode');
      pt_register('POST','Email');
      pt_register('POST','SelectClass');
      pt_register('POST','SelectTime');
      pt_register('POST','SelectDay');
      pt_register('POST','SelectLevel');
      pt_register('POST','PaymentTransactionNo');
      if($Name=="" || $Address1=="" || $State=="" || $Zipcode=="" || $Email=="" || $SelectClass=="" || $SelectTime=="" || $SelectDay=="" || $SelectLevel=="" || $PaymentTransactionNo=="" ){
      $errors=1;
      $error.="<li>You did not enter one or more of the required fields. Please go back and try again.";
      }

       
      • TNTEverett

        TNTEverett - 2007-04-09

        What is the point.  You have all options required except Address2.  What are you counting, and why?

         
    • ricx

      ricx - 2007-04-09

      Okay. Here's what I did. "SelectClass", "SelectTime" and "SelectDay" are checkbox categories, each with its own set of checkboxes. Since the program did not generate these checkboxes so I did it manually after the form has been generated. (I also tried creating them as radio buttons first and then change them to checkbox in the code). Anyways, the checkboxes do display on the form but the problem is when I test the form and say if I check off 2 checkboxes for "SelectClass", the resulting data in the table only display the 1st checkbox option, ignoring the 2nd option.

      On the 1st posting above, you instructed the poster to place this "variable count" codes on to the process.php which I thought is something like what I was doing too. So I tried doing what you told him but it didn't work for me.

      So can you give me some advice where to put these codes or if I'm doing it wrong how should I do it then? Thanks.

       
      • TNTEverett

        TNTEverett - 2007-04-09

        Okay so you have added checkboxes that are not represented in your phpFormgenerated form.  This means that anything you added can not be checked or displayed later.  The first step you must do is to either start over and make sure all your options are generated from the beginning, or modify the process.php file to include the new variables you added. 

        Example:
        pt_register('POST','PaymentTransactionNo');

        if ($SelectClass){$selectclasscnt=1};
        if ($selectclasscnt == 0){$errors=1; $error.="Group1: You Must select at least one.!";}

        if($Name=="" || $Address1=="" || $State=="" || $Zipcode=="" || $Email=="" || $SelectClass=="" || $SelectTime=="" || $SelectDay=="" || $SelectLevel=="" || $PaymentTransactionNo=="" ){
        $errors=1;
        $error.="<li>You did not enter one or more of the required fields. Please go back and try again.";
        }

        The above example is just a crude example.  You must define the variables to be counted and the acceptable limits of the count. 

         

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.