Dependent questions (subquestions)

an andrea2 16 Nov, 2014
Hello everybody,
I'm new here. I have some doubts, I hope you can help me. I apologize in advance if this question has already been done, but I didn't find an answer in my searches.
I want to create a dependent question structure, like this:

Q1: Do you like cats? Yes/No
Q2 (Q1=Yes): Which kind of cat do you like most?
Q2 (Q1=No): Why do you dislike cats?

...and so on. At the end of each path I want to show a html text and have a email sent. And I really wish to have a single question per page (something similar to the "goto page based on answer" option in google forms).
So, I know there is a demo-fields-event and I have already analyzed it, but it doesn't work. I mean, it doesn't work the demo itself, and so my try doesn't work too. It's weird, because in the same demo there is the checkbox element which has similar properties related to the submit button, and it work perfectly.
So, may you help me please? Sincerely I'm not sure creating a structure similar to the demo-fields-event would be the best choice to reach my goal, so I'm open to any suggestions.
Thank you!
andrea
Gr GreyHead 16 Nov, 2014
Hi Andrea,

This really is a case for custom JavaScript to encode the question sequence that you have in mind. You can do fairly simple structured questions with the built-in event handler but only really one level of a question tree.

Note that there was a bug in one version that broke the radio buttons, I think that is now fixed if you upgrade to the latest version.

Bob
Max_admin Max_admin 16 Nov, 2014
1 Likes
Hi,

Unfortunately the radio event bug is still not yet fixed in the public release, but you can get the patch file by sending a message using the "Contact us" page.

Regards,
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
an andrea2 16 Nov, 2014
Thank you!!!

I have just send you a message for the patch file using the "Contact us" page.

Sorry, I'm a newbie. On the Setup tab I noticed there is a LoadJavaScript element, so I guess I should use it but I have no idea which code I have to put in and where to place the element. Please, could you provide me with a example code?

Thank you again!
Gr GreyHead 16 Nov, 2014
Answer
Hi Andrea,

I've posted an example below; it's the JavaScript of a 'wizard' form that I wrote a while ago using MooTools in CFv4. You'll see that it is very repetitive but not trivial. The script hide and shows whole blocks of questions depending on the answers given to a series of questions - radio buttons mostly.

Bob

window.addEvent('domready', function(){
  $$('.javascript_off').each(function(el){
    el.show();
  });
  $$('.javascript_on').each(function(el){
    el.hide();
  });
  $$('#current_qual_container_div input').each(function(el){
    el.addEvent('click', changeQual);
  });
  $$('#pr_experience_container_div input').each(function(el) {
    el.addEvent('click', changeExp);
  });
  $$('#nctj_experience_container_div input').each(function(el) {
    el.addEvent('click', changeExp);
  });
  $$('#is_degree_container_div input').each(function(el) {
    el.addEvent('click', changeDegree);
  });
  $$('#has_degree_container_div input').each(function(el) {
    el.addEvent('click', changeDegree);
  });

  function changeQual(){
    $$('.qualify_for div').each(function(el){
      el.hide();
    });
    $$('#questions input').each(function(el) {
      el.checked = false;
    });
    $$('.questions div.cfdiv_radio').each(function(el){
      el.hide();
    });
    // CIPR Foundation
    if ( $('current_qual_0').checked ) {
      $('pr_experience_container_div').show();
    }
    // CIM Diploma
    if ( $('current_qual_1').checked ) {
      $('pr_experience_container_div').show();
    }
    // NCTJ Diploma
    if ( $('current_qual_2').checked ) {
      $('nctj_experience_container_div').show();
    }
    // CIPR advanced
    if ( $('current_qual_3').checked ) {
      $('pr_experience_container_div').show();
    }
    // CIPR Diploma
    if ( $('current_qual_4').checked ) {
      $('qual_specific').show();
    }
    // Short specific CIPR
    if ( $('current_qual_5').checked ) {
      $('qual_main_diploma').show();
    }
    // Has degree
    if ( $('current_qual_6').checked ) {
      $('pr_experience_container_div').show();
    }
    // None of the above
    if ( $('current_qual_7').checked ) {
      $('pr_experience_container_div').show();
    }
  }

  function changeExp(){
    $$('.qualify_for div').each(function(el){
      el.hide();
    });

    if ( $('current_qual_0').checked ) {
      // CIPR Foundation
      if ( $('pr_experience_0').checked ) {
        // None
        $('qual_certificate').show();
      }
      if ( $('pr_experience_1').checked ) {
        // less than 12 months
        $('qual_certificate').show();
      }
      if ( $('has_degree_0').checked ||
          $('has_degree_1').checked ) {
        // degree q already answered
        changeDegree();
      } else {
        // show degree question
        if ( $('pr_experience_2').checked ) {
          // more than 12 months
          $('has_degree_container_div').show();
        }
        if ( $('pr_experience_3').checked ) {
          // more than 2 years
          $('has_degree_container_div').show();
        }
      }
      if ( $('pr_experience_4').checked ) {
        // more than 4 years
        $('qual_diploma').show();
      }
    }

    if ( $('current_qual_1').checked ) {
      // CIM Diploma
      if ( $('pr_experience_0').checked ) {
        // None
        $('qual_12_exp').show();
        $('qual_adv_certificate').show();
      }
      if ( $('pr_experience_1').checked ) {
        // less than 12 months
        $('qual_certificate').show();
      }
      if ( $('pr_experience_2').checked ) {
        // more than 12 months
        $('qual_diploma').show();
      }
      if ( $('pr_experience_3').checked ) {
        // more than 2 years
        $('qual_diploma').show();
      }
      if ( $('pr_experience_4').checked ) {
        // more than 4 years
        $('qual_diploma').show();
      }
    }
    // NCTJ Diploma
    if ( $('current_qual_2').checked ) {
      // CIM Diploma
      if ( $('nctj_experience_0').checked ) {
        // No
        $('qual_adv_certificate').show();
      }
      if ( $('nctj_experience_1').checked ) {
        // Yes
        $('qual_diploma').show();
      }
    }

    // CIPR advanced cert
    if ( $('current_qual_3').checked ) {
      if ( $('pr_experience_0').checked ) {
        // None
        //$('qual_12_exp').show();
        $('qual_diploma').show();
      }
      if ( $('pr_experience_1').checked ) {
        // less than 12 months
        //$('qual_12_exp').show();
        $('qual_diploma').show();
      }
      if ( $('pr_experience_2').checked ) {
        // more than 12 months
        $('qual_diploma').show();
      }
      if ( $('pr_experience_3').checked ) {
        // more than 2 years
        $('qual_diploma').show();
      }
      if ( $('pr_experience_4').checked ) {
        // more than 4 years
        $('qual_diploma').show();
      }
    }

    // Degree
    if ( $('current_qual_6').checked ) {
      if ( $('pr_experience_0').checked ) {
        // None
        $('qual_adv_certificate').show();
      }
      if ( $('pr_experience_1').checked ) {
        // less than 12 months
        $('qual_adv_certificate').show();
      }
      if ( $('is_degree_0').checked ||
          $('is_degree_1').checked ) {
        // degree q already answered
        changeDegree();
      } else {
        // show degree question
        if ( $('pr_experience_2').checked ) {
          // more than 12 months
          $('is_degree_container_div').show();
        }
        if ( $('pr_experience_3').checked ) {
          // more than 2 years
          $('is_degree_container_div').show();
        }
        if ( $('pr_experience_4').checked ) {
          // more than 4 years
          $('is_degree_container_div').show();
        }
      }
    }

    // None of the above
    if ( $('current_qual_7').checked ) {
      if ( $('pr_experience_0').checked ) {
        // None
        $('qual_foundation').show();
      }
      if ( $('pr_experience_1').checked ) {
        // less than 12 months
        $('qual_foundation').show();
      }
      if ( $('pr_experience_2').checked ) {
        // more than 12 months
        $('qual_foundation').show();
      }
      if ( $('pr_experience_3').checked ) {
        // more than 2 years
        $('qual_adv_certificate').show();
      }
      if ( $('pr_experience_4').checked ) {
        // more than 4 years
        $('qual_diploma').show();
      }
    }
  }

  function changeDegree(){
    $$('.qualify_for div').each(function(el){
      el.hide();
    });
    // CIPR Foundation
    if ( $('current_qual_0').checked ) {
      if ( $('has_degree_0').checked ) {
        // non PR related degree
        if ( $('pr_experience_0').checked ) {
          // None
          $('qual_certificate').show();
        }
        if ( $('pr_experience_1').checked ) {
          // less than 12 months
          $('qual_certificate').show();
        }
        if ( $('pr_experience_2').checked ) {
          // more than 12 months
          $('qual_certificate').show();
        }
        if ( $('pr_experience_3').checked ) {
          // more than 2 years
          $('qual_adv_certificate').show();
        }
        if ( $('pr_experience_4').checked ) {
          // more than 4 years
          $('qual_diploma').show();
        }
      }
      if ( $('has_degree_1').checked ) {
        // PR related degree
        $('qual_diploma').show();
      }
    }

    // Degree
    if ( $('current_qual_6').checked ) {
      if ( $('is_degree_0').checked ) {
        // non PR related degree
        if ( $('pr_experience_0').checked ) {
          // None
          $('qual_adv_certificate').show();
        }
        if ( $('pr_experience_1').checked ) {
          // less than 12 months
          $('qual_adv_certificate').show();
        }
        if ( $('pr_experience_2').checked ) {
          // more than 12 months
          $('qual_adv_certificate').show();
        }
        if ( $('pr_experience_3').checked ) {
          // more than 2 years
          $('qual_diploma').show();
        }
        if ( $('pr_experience_4').checked ) {
          // more than 4 years
          $('qual_diploma').show();
        }
      }
      if ( $('is_degree_1').checked ) {
        // PR related degree
        $('qual_diploma').show();
      }
    }
  }
});
an andrea2 16 Nov, 2014
Thank you!!
The patch works and the built-in event manager should be enough for my goal, for now.
And thank you GreyHead for the code, I have not yet used it but it could be useful for my future projects.
Have a good day!
andrea
This topic is locked and no more replies can be posted.