Timer on Forms

micina90

Member
Hello all,

We have a test done with Fabrikar and we would like a timer done. Basically we would like this:

Have all the questions hidden and once a button called START is clicked the timer (of 45 mins) will start decreasing and the questions visible. We would also like that the user is notified when he/she will have 30, 15, 10, 5 mins left. And once the timer is done, the user will be redirected to a page stating that time is up. Or hide the questions and have a re-start button.

Thanks
 
Thanks :)

so yeah I managed to do a small timer once you hit start

Now is it possible to send the form once the timer hits 00:00 ?

I do have some validations in the form ...
 
Also I'm trying to this thing to submit a form using .submit() ... but I'm having a problem.

Is it possible to run a validation of a particular element on a button element?

What I'm trying to do is this:
Check that a value from the dropdown is selected (being done with JS)
Check that a value is entered in the Full Name (being done with JS) etc BUT

I cannot check captcha with JS ... or there's a way to do it and I dont know how ... is it possible for you guys to lend me a hand?

Thanks
 
Also I'm trying to this thing to submit a form using .submit() ... but I'm having a problem.

Is it possible to run a validation of a particular element on a button element?

What I'm trying to do is this:
Check that a value from the dropdown is selected (being done with JS)
Check that a value is entered in the Full Name (being done with JS) etc BUT

I cannot check captcha with JS ... or there's a way to do it and I dont know how ... is it possible for you guys to lend me a hand?

Thanks
Why not just add a line of javascript to click the submit button when the timer expires. Then any form validations you have entered via fabrik should kick in and there's no need for you to write any special javascript for it.
 
I can't because all the validation should be done BEFORE the test.

So I have this:
GROUP 1
Name
Email
Captcha
START TEST

GROUP 2 w/ test questions.

So Group 2 is hidden on form load. As soon as you click START TEST (which is a button element and I don't want to submit the form on this button - i just want to run the validation) a validation for the name and email is done (which is easy using normal JS) but I want the captcha to be validated as well so the timer can start after START TEST and have the user to do test and then submit the whole form. So the validation CANNOT fail on the submit button - therefore it must be validated on the START button
 
Sorry, I missed that.

My first thought when I read your #5 post was that I don't think it's a good idea to use javascript for captcha validation. By validating your captcha on the user end you are defeating the main purpose of captcha.

I need something similar to this myself so I tried to come up with something for you. About the only thing I didn't do was create a timer (but you've already got that code working).

My thought was, why not have a hidden element to act as a flag (default 0) - and set to 1 once those name/email/captcha elements are valid ? Then use that flag to determine what gets shown/hidden.
I tried this and it seems to work ok...

In my case - and with the examples below - I wanted to hide the entire joined group.
The parent table name is 'test_list'.
The full name of the captcha element is 'test_list___captcha_verify'.
The full name of the button element is 'test_list___test_button'.
The name of the validation flag toggle element (INT, 1) is 'test_list___captcha_valid'.

First add some javascript in the document ready section of the form_xxx.js file that will...
1. Hide the default form submit/apply button(s) if captcha_valid <> '1'
2. Set the visibility of the hidden elements to be determined by captcha_valid == '1'
3. Set the start timer ticking if captcha_valid == '1'
4. Hide the 'Start Test' button if captcha_valid == '1'

example...
Code:
    var captcha_valid = jQuery("#test_list___captcha_valid").val();
    if(captcha_valid !== '1'){
        // hide the default submit button
        jQuery("button.save").hide();
        // hide the elements you want hidden until validation
        // in this case I want to hide the entire joined group
        jQuery("fieldset[id=group327]").hide();
    }
    else
    {
        // if captcha_valid == '1'...
        // Hide the 'Start Test' button
        jQuery("#test_list___test_button").hide();
        // Set the start timer ticking
        // Your start timer code would go here
    }

Then add additional javascript as an onClick trigger (also in the document ready section of the form_xxx.js file) for your 'Start Test' button so that when clicked...
1. The submit button gets renamed to 'start_submit' (as a way of identifying how the form was submitted)
2. Trigger a click on the default form submit button
Code:
    jQuery(document).on("click","#test_list___test_button",function(){
        jQuery("button.save").attr("name", "start_submit");
        jQuery("button.save").click();
    });
Use the php form plugin to add 3 shorts scripts
1. Process script: Start of form submission (onBeforeProcess) - set the 'captcha_valid' element value to 1.
PHP:
$formModel->updateFormData("test_list___captcha_valid","1");
2. Process script: If an error occurs in form submission (onError) - set the 'captcha_valid' element value back to 0. (And user will be redirected back to the form because of error.)
PHP:
$formModel->updateFormData("test_list___captcha_valid","0");
3. Process script: End of form submission (onAfterProcess) - if the 'Start Test' button was used to trigger the form submission and the form validated, then redirect user back to the same form.
PHP:
if(isset($_POST['start_submit'])){
  $app = JFactory::getApplication();
  $return = JURI::getInstance()->toString();
  $app->redirect($return, 'Validation Approved! Timer has begun.', $msgType='message');
}
The way this works is, if the name/email/captcha validates, the form is repainted with the hidden elements included and the timer ticking. And if it doesn't validate, the user is returned to the same form without the hidden elements (or timer ticking) with the message 'Some parts of your form have not been correctly filled in' and the invalid elements (including the captcha) shown in red.

The one big roadblock I had with this (similar to the root of your original question, I suppose) was hiding the captcha element. Because it is a plugin it can't simply be just hidden or disabled by javascript. (I even tried using remove() to remove the entire element div from the DOM - but the plugin would just keep adding it back.)

So what I had to do was hack the fabrik core to add a few lines of code. That could be eliminated if someone who better understands the fabrik core would add another option to the captcha element. (Just under the option to disable captcha if the user is logged in?) Perhaps labeled "Disable when" - and then allow entry of a logical argument that would determine if/when the captcha is shown - e.g. using wildcards - similar to the 'Order by/Where' clause in the configuration of a database join element.

I know that's just a pipe dream for now - but anyhow, this is where that 'Disable when' would be used and the code would need to be changed - in the /plugins/fabrik_element/captcha/captcha.php file at line 148 - just above the last line in public function canUse() ...
(That function is used to determine whether to use the captcha plugin.)
PHP:
$thisForm = PlgFabrik_Element::getFormModel(); 
$thisFormData = $thisForm->getData();
if($thisFormData['test_list___captcha_valid'] == '1'){ 
    return false; 
}
I inserted the above code there and it works like a charm.

I suppose maybe this could also all be done with ajax - but I'm not familiar enough with using ajax in fabrik to help you with that.
 
Sorry, I missed that.

My first thought when I read your #5 post was that I don't think it's a good idea to use javascript for captcha validation. By validating your captcha on the user end you are defeating the main purpose of captcha.

I need something similar to this myself so I tried to come up with something for you. About the only thing I didn't do was create a timer (but you've already got that code working).

My thought was, why not have a hidden element to act as a flag (default 0) - and set to 1 once those name/email/captcha elements are valid ? Then use that flag to determine what gets shown/hidden.
I tried this and it seems to work ok...

In my case - and with the examples below - I wanted to hide the entire joined group.
The parent table name is 'test_list'.
The full name of the captcha element is 'test_list___captcha_verify'.
The full name of the button element is 'test_list___test_button'.
The name of the validation flag toggle element (INT, 1) is 'test_list___captcha_valid'.

First add some javascript in the document ready section of the form_xxx.js file that will...
1. Hide the default form submit/apply button(s) if captcha_valid <> '1'
2. Set the visibility of the hidden elements to be determined by captcha_valid == '1'
3. Set the start timer ticking if captcha_valid == '1'
4. Hide the 'Start Test' button if captcha_valid == '1'

example...
Code:
    var captcha_valid = jQuery("#test_list___captcha_valid").val();
    if(captcha_valid !== '1'){
        // hide the default submit button
        jQuery("button.save").hide();
        // hide the elements you want hidden until validation
        // in this case I want to hide the entire joined group
        jQuery("fieldset[id=group327]").hide();
    }
    else
    {
        // if captcha_valid == '1'...
        // Hide the 'Start Test' button
        jQuery("#test_list___test_button").hide();
        // Set the start timer ticking
        // Your start timer code would go here
    }

Then add additional javascript as an onClick trigger (also in the document ready section of the form_xxx.js file) for your 'Start Test' button so that when clicked...
1. The submit button gets renamed to 'start_submit' (as a way of identifying how the form was submitted)
2. Trigger a click on the default form submit button
Code:
    jQuery(document).on("click","#test_list___test_button",function(){
        jQuery("button.save").attr("name", "start_submit");
        jQuery("button.save").click();
    });
Use the php form plugin to add 3 shorts scripts
1. Process script: Start of form submission (onBeforeProcess) - set the 'captcha_valid' element value to 1.
PHP:
$formModel->updateFormData("test_list___captcha_valid","1");
2. Process script: If an error occurs in form submission (onError) - set the 'captcha_valid' element value back to 0. (And user will be redirected back to the form because of error.)
PHP:
$formModel->updateFormData("test_list___captcha_valid","0");
3. Process script: End of form submission (onAfterProcess) - if the 'Start Test' button was used to trigger the form submission and the form validated, then redirect user back to the same form.
PHP:
if(isset($_POST['start_submit'])){
  $app = JFactory::getApplication();
  $return = JURI::getInstance()->toString();
  $app->redirect($return, 'Validation Approved! Timer has begun.', $msgType='message');
}
The way this works is, if the name/email/captcha validates, the form is repainted with the hidden elements included and the timer ticking. And if it doesn't validate, the user is returned to the same form without the hidden elements (or timer ticking) with the message 'Some parts of your form have not been correctly filled in' and the invalid elements (including the captcha) shown in red.

The one big roadblock I had with this (similar to the root of your original question, I suppose) was hiding the captcha element. Because it is a plugin it can't simply be just hidden or disabled by javascript. (I even tried using remove() to remove the entire element div from the DOM - but the plugin would just keep adding it back.)

So what I had to do was hack the fabrik core to add a few lines of code. That could be eliminated if someone who better understands the fabrik core would add another option to the captcha element. (Just under the option to disable captcha if the user is logged in?) Perhaps labeled "Disable when" - and then allow entry of a logical argument that would determine if/when the captcha is shown - e.g. using wildcards - similar to the 'Order by/Where' clause in the configuration of a database join element.

I know that's just a pipe dream for now - but anyhow, this is where that 'Disable when' would be used and the code would need to be changed - in the /plugins/fabrik_element/captcha/captcha.php file at line 148 - just above the last line in public function canUse() ...
(That function is used to determine whether to use the captcha plugin.)
PHP:
$thisForm = PlgFabrik_Element::getFormModel();
$thisFormData = $thisForm->getData();
if($thisFormData['test_list___captcha_valid'] == '1'){
    return false;
}
I inserted the above code there and it works like a charm.

I suppose maybe this could also all be done with ajax - but I'm not familiar enough with using ajax in fabrik to help you with that.

Cheers for your help !! But I skipped captcha ... We are making the user register first and logging in before he could take the test. I still used some bits and pieces of your code though but I can't get the submit button to submit when timer reacher 0:00 :(
 
Back
Top