Forums

Hide/Show sections vs. Element Switcher

HFeimster 02 Jun, 2014
Hi Folks,

I'm trying to create a contact form with some built in filters for an HR department. The attached pdf shows the flow chart they sent. [attachment=0]HRworkflow.pdf[/attachment] There are basically a set of "checks" built in to eliminate applicants who don't meet the basic requirements and send them an automated reply. Applicants who meet the basic threshold will be sent on to the HR dept contact.

Ideally, I'd like users of the form to only see the Name, Email and Birth Year fields. If the birth year falls outside of the appropriate range, then they submit (without being asked for more info) and they are sent an email saying thanks, but we have no openings for them at this time.

If they do meet the age restrictions, then the next set of questions appear, and the show/hide happens again with the Nationality field. And then again with the Education/Degree field.

I'm lost on whether to achieve this through the Element Switcher [GH] plugin, or through the Shoe/Hide Section/div process that I've seen on here while I've been researching. I'm pretty new to dynamic forms (anything other than a basic contact form) so I could use any super basic tips (like how would I set up the fields/actions/elements/etc to do the show/hide thing?).

I appreciate any feedback or suggestions! Thanks!
Max_admin 02 Jun, 2014
Hi HFeimster,

The easiest way to do this is by doing a conditional multi page form, first page with the 3 fields, then the rest, and in between you should use an "Event switcher" to switch the pages processing, this is explained on some forum topic here for v5 but I couldn't find the topic now.

Regards,
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
GreyHead 02 Jun, 2014
Hi HFeimster,

There are a couple of ways of doing this.

One is to use containers for each block of questions and then use JavaScript to show/hide the containers depending on the previous answers. This can be effective for branched questionnaires but takes some JavaScript knowledge to get working.

The second, and simpler, one, which I would recommend here, is to make this into a multi-part form. Make each section a separate form; then use PHP with an event switcher in the On Submit Action to either show a 'Thanks but no thanks' message or to display the next form in the sequence. Please see this FAQ and this FAQ

Bob

PS You could also use a multi-page form to keep it all in one form but I think that adding the conditions are trickier if you do that.
HFeimster 02 Jun, 2014
Thanks for the responses, guys! I'll look over those resources and see what would work the best. I will come back to you with any more questions once I do.

Bob - When you say "use PHP with an event switcher", where exactly do you add the PHP script? Is it inside the event switcher action or is it a separate action before the event switcher? Thanks for clarifying - just trying to wrap my brain about the logic behind the code.

Thanks - Heather
HFeimster 02 Jun, 2014
Alright - two questions have occurred to me:

1 - According to the FAQ page on long forms, "You can break multi-page forms into separate forms each of more manageable size. You can then use the 'Session to Data' and 'Data to Session' actions to pass data between them." If I break the form into multiple forms, will the final “submit” be able to pull fields from each form? Or do I put in “hidden” fields into the subsequent forms and pre-fill them with the information they already submitted?

2 - Do I need to set up a separate MySQL DB for this sort of thing to work properly?
Thanks - H
GreyHead 02 Jun, 2014
Hi Heather,

It depends on the Event Switcher, my Event Switcher [GH] action uses PHP inside the action.

Bob
HFeimster 02 Jun, 2014
Hi Bob - Thanks. I already have your Event Switcher [GH] installed.

Is there anywhere could I find examples of the PHP code needed for the action? The age one is the trickiest - the condition would need to be any number "greater than" / "less than". For the others, I've put in an "Other" option to trigger the email and stop the progression to the next form.
GreyHead 02 Jun, 2014
Hi Heather,

There are two parts to the age check:

a) convert the date of birth into an age. There's some code in this StackOverflow answer that will do that for you.

b) check the age. That would be something like:
<?php
if ( $age < 18 ) {
  return 'event_a';
} elseif  ( $age > 59 ) {
  return 'event_b';
} else {
  return 'event_c';
}
?>
Then you can use events a and b to send appropriately worded emails (use an Email action followed by a Show Stopper action) and event c to redirect to the next step (use a Show Form action).

If you want to include the age in the emails then add $form->data['age'] = $age; before the if( statement and put {age} in the email template.

Bob
HFeimster 02 Jun, 2014
You totally rock! That's exactly what I'm looking for. I'm going to give it a shot at building this once my flu-ridden toddler goes to bed. He makes it a little difficult to concentrate!

I'll send an update with the link once I get through it all. Thanks. -H-
HFeimster 02 Jun, 2014
Alright - So we've made some progress...

Still hitting a snag with the age calculation. The age calculator is working (I forgot to remove the echo initially and it showed the correct age), I'm just not getting it to move on to the next action - it's like it isn't using the correct age parameter for the Event Switcher script.

I had it in it's own Custom Code action to begin with, above the Event Switcher. When that didn't work, I tried putting it inside the Event Switcher, before the IfElse, but that still isn't working.

Any thoughts on what I'm doing wrong here? Is there a step I'm missing to enable the system to use the $age parameter?

Here's a link to the first form: http://dev.isg.edu.sa/index.php?option=com_chronoforms&chronoform=SponsoredApplicants1

Here's what I put for the Event Switcher (age calc and If..Else)
<?php

  //explode the date to get month, day and year
  $birthDate = explode("/", $birthDate);
  //get age from date or birthdate
  $age = (date("md", date("U", mktime(0, 0, 0, $birthDate[2], $birthDate[1], $birthDate[0]))) > date("md")
    ? ((date("Y") - $birthDate[0]) - 1)
    : (date("Y") - $birthDate[0]));

    if ( $age < 18 ) {
      return 'event_a';
    } 

elseif  ( $age > 59 ) {
      return 'event_b';
    } 

else {
      return 'event_c';
    }

?>
GreyHead 02 Jun, 2014
Hi Heather,

You have to get the date value from the $form->data[] array where ChronoForms put the values (use a debugger action to see what is there).

Here's a modified version of your code that should make the logic clearer. you may need to replace birthdate with the name of your date input; and possibly change the $dob[2], $dob[1], $dob[0] around depending on the format of your date - I think they are assuming yy/mm/dd here
<?php
/**
* Class and Function List:
* Function list:
* Classes list:
*/

//explode the date to get month, day and year
$dob = explode('/', $form->data['birthDate']);

//get age from date or birthdate
$age = date('Y') - $dob[0];
$dob_md = date('md', date('U', mktime(0, 0, 0, $dob[2], $dob[1], $dob[0])));
$now_md = date('md');
if ( $dob_md > $now_md ) {
  $age = $age - 1;
}
if ( $age < 18 ) {
  return 'event_a';
} elseif ($age > 59) {
  return 'event_b';
} else {
  return 'event_c';
}
?>

Bob
HFeimster 02 Jun, 2014
Thanks! That's EXACTLY what I was trying to figure out. I just didn't know the syntax of how to tell it to look inside the form to get the field data.

I tweaked the script to match the YYYY-MM-DD format that the MooTools datepicker uses.

This is what I changed my code to in the Event Switcher:
<?php

  //explode the date to get month, day and year
  $birthDate = explode("/", $form->data['birthdate'];
  //get age from date or birthdate
  $age = (date("md", date("U", mktime(0, 0, 0, $birthdate[2], $birthDate[1], $birthDate[0]))) > date("md")
    ? ((date("Y") - $birthDate[0]) - 1)
    : (date("Y") - $birthDate[0]));

?>

<?php

    if ( $age < 18 ) {
      return 'event_a';
    } 

elseif  ( $age > 59 ) {
      return 'event_b';
    } 

else {
      return 'event_c';
    }

?>


It looks like it's working okay - it's no longer loading the "Thanks" message which tells me it isn't stuck in the Event A or Event B which is good. However, it also isn't loading the next form. Attached is a screenshot of my Actions/Events for the first form. I feel like I'm missing something - some trigger to load the next form.

PS - Thanks for all the help!
GreyHead 02 Jun, 2014
Hi Heather,

Is the show form action configured to point to the next form and event? Everything in the image looks OK.

Adding a Debugger action temporarily may give you some more clues.

You can also try putting the show form action after the event switcher (and remove the else clause from the PHP)

Bob
HFeimster 03 Jun, 2014
Hey Bob - Thanks for the great tips last night. I moved the Show Form to after the Event Switcher and it worked last night ... but for some reason, something is not working today.

Link to the test form: http://dev.isg.edu.sa/index.php?option=com_chronoforms&chronoform=SponsoredApplicants1

It's like it's skipping the Event Switcher entirely and just pulling up the new form.

Here is my Event Switcher code - I don't think I changed anything but maybe I did?
<?php

  //explode the date to get month, day and year
  $birthDate = explode("/", $form->data['birthdate'];
  //get age from date or birthdate
  $age = (date("md", date("U", mktime(0, 0, 0, $birthdate[2], $birthDate[1], $birthDate[0]))) > date("md")
    ? ((date("Y") - $birthDate[0]) - 1)
    : (date("Y") - $birthDate[0]));


    if ( $age < '18' ) {
      return 'event_a';
    } 

elseif  ( $age > '59' ) {
      return 'event_b';
    } 

?>


And here is the debugger code shown after the On Submit (when it loads the next form which it shouldn't):
Array
(
    [option] => com_chronoforms
    [chronoform] => SponsoredApplicants1
    [event] => submit
    [Itemid] => 
    [first_name] => Tester
    [last_name] => Twelve
    [email] => heather.feimster@gmail.com
    [birthdate] => 2006-05-09
    [input_submit_12] => Submit
    [ceae2e520d29bc29289343682b1e44bc] => 1
)


Don't you just love when code works at one point, then overnight wigs out? 😉 Thanks for any help with this.

FYI - I was messing with the email settings this morning, trying to get them to work. I think my problem is that the IT guy keeps giving me an email address for our old domain name (which now points to the new one) instead of one with the new domain (where the site is hosted). Could you just confirm - this is gonna be a problem if the system email address is not under the same domain, right? (Just trying to get more ammo to show him why I need the new domain...)
Max_admin 03 Jun, 2014
I'm not sure what does this line do:
$age = (date("md", date("U", mktime(0, 0, 0, $birthDate[2], $birthDate[1], $birthDate[0]))) > date("md")
    ? ((date("Y") - $birthDate[0]) - 1)
    : (date("Y") - $birthDate[0]));

in which format the date is stored ?

Also if its skipping the "Event switcher" completely then maybe the age is > 18 and less than 59

Regards,
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
GreyHead 05 Jun, 2014
Hi,

@Max, That line should covert a date of birth into an age in years. Mostly it checking if this year's birthday is in the past or future.

@Heather, Looks OK from here but there may be a typo in your code. I'm away from my desk and can't simply check for any typos. Or, as Max says, it may be that they all meet the test criteria.

Bob
This topic is locked and no more replies can be posted.