TOPIC:

SOLVED! Coding for Save Value on Submit 1 week 3 days ago #249486

  • Topic Author
  • sprockt012
  • Offline
  • Senior Breezer
  • Senior Breezer
  • Posts: 96
  • Thanks: 0
I posted a similar post within BreezingForms before seeing this Coding area. My issue is definitely with coding. I need a field value to be determined based upon the value of two other field values.

So far I have two partially functioning solutions but neither is getting me to a working solution. I'm open to having the field calculate with a custom action script upon submit or within Begin Submit - I just think my code is wrong in both instances so whichever is closest and easiest to "fix" works for me.

Option 1 - Custom Action Script runs at Submit

I have a function that's being called upon clicking my Submit button that runs a custom script on the Status field (see code below).

However it's not working EXACTLY right. Some of the values I'd expect to see in a situation aren't correct. Ultimately I want the field value for Status to calculate as:
  • Incomplete if either checkbox or sum isn't filled (ie. 0 value);
  • Bad if checkbox or sum is greater than 3; or
  • Good if checkbox and sum are both =3:
function ff_idbfStatus_action(element, action)

{
if (ff_getElementByName('idbfSum').value > '3' || ff_getElementByName('idbfCheckbox').value > '3');
   {
ff_getElementByName('idbfStatus').value = 'Bad';
}
if (ff_getElementByName('idbfSum').value == '0' || ff_getElementByName('idbfCheckbox').value == '0');
   {
ff_getElementByName('idbfStatus').value = 'Incomplete';
}
if (ff_getElementByName('idbfSum').value == '3' && ff_getElementByName('idbfCheckbox').value == '3');
   {
ff_getElementByName('idbfStatus').value = 'Good';
}
}

The behavior I see now that's wrong for example is:
If I click Submit when the form is empty, Status is calculating as "Good"
If I click Submit when values greater than 3 are selected, Status is calculating as "Good"

This script below worked perfectly and it was when I tried to add the "or" and the "and" to include the second checkbox field that I need to determine the value it started acting up:
function ff_idbfStatus_action(element, action)

{
if (ff_getElementByName('idbfSum').value == 3)
   {
ff_getElementByName('idbfStatus').value = 'Good';
}
if (ff_getElementByName('idbfSum').value > 3)
   {
ff_getElementByName('idbfStatus').value = 'Bad';
}
if (ff_getElementByName('idbfSum').value == 0)
   {
ff_getElementByName('idbfStatus').value = 'Incomplete';
}
}


Option 2 - Begin Submit
This code below is likely messy and I have no idea where to go from here. I built it based off code used prior for calculating and saving a field.
/* Calculate and Save Status */

 /* update status based on sum and checkbox values */

$this->execPieceByName('ff_InitLib');

$status = '';

    if( $data[_FF_DATA_NAME] == 'idbfStatus'){
        $status = $data[_FF_DATA_VALUE];
    }

/*  $status = 'Incomplete'; */
foreach( $this->savedata As $data ){

 $this->savedata[$status][_FF_DATA_VALUE] = 'Incomplete'; 

/* look for positive values: */
if( ff_getSubmit("idbfSum") >= "4" or ff_getSubmit("idbfSymptomsCheckbox") >= "4"){
     
     $this->savedata[$status][_FF_DATA_VALUE] = 'Positive'; 
   }

/* look for good values */
if( ff_getSubmit("idbfSum") == "3" and ff_getSubmit("idbfSymptomsCheckbox") == "3"){
     
     $this->savedata[$status][_FF_DATA_VALUE] = 'Good'; 
   }

/* $clear = array(); */
$this->savedata;
}

Please Log in or Create an account to join the conversation.

Last edit: Post by sprockt012.

SOLVED! Coding for Save Value on Submit 1 week 3 days ago #249489

  • Topic Author
  • sprockt012
  • Offline
  • Senior Breezer
  • Senior Breezer
  • Posts: 96
  • Thanks: 0
I've finally got this working - I found that the major issue with the methods I was using was that the checkbox group didn't seem to actually have a value which meant the script couldn't recognize the value of that field. Based on what I found in another forum post about creating a total for checkboxes, I used that example in my code and got it to work. Here are the steps and code if anyone else is interested:

* Already had a hidden field (read only & turned off) named idbfSum that totals the values from six radio button options on my form (those fields have onclick action script)
1. Created hidden field (read only & turned off) named idbfStatus on page 1 with Actionscript > Custom > no action selected:
function ff_idbfStatus_action(element, action)

{
if (ff_getElementByName('idbfSum').value == 3)
   {
ff_getElementByName('idbfStatus').value = 'Good';
}
if (ff_getElementByName('idbfCheckboxTotal').value == 3)
   {
ff_getElementByName('idbfStatus').value = 'Good';
}
if (ff_getElementByName('idbfSum').value >= 4)
   {
ff_getElementByName('idbfStatus').value = 'Bad';
}
if (ff_getElementByName('idbfCheckboxTotal').value >= 4)
   {
ff_getElementByName('idbfStatus').value = 'Bad';
}
if (ff_getElementByName('idbfSum').value == 0)
   {
ff_getElementByName('idbfStatus').value = 'Incomplete';
}
if (ff_getElementByName('idbfCheckboxTotal').value == 0)
   {
ff_getElementByName('idbfStatus').value = 'Incomplete';
}

}

2. Created a text field (read only & turned off) on page 1 named idbfCheckboxTotal
3. Created a button element on page 1 with value Continue with Actionscript > Custom > OnClick which will run script to generate Status.
function ff_idbfNextButton_action(element, action)
{
    switch (action) {
        case 'click':
             ff_idbfStatus_action(element, action);
              CHOOSE   ff_validate_submit();      OR       ff_validate_nextpage(); 
            break;
        default:;
    } // switch
} // ff_idbfNextButton_action

4. In Form properties disabled Paging as custom onclick Continue button will replace it
5. Form > Advanced > Script > Initialization > Custom (will total checkbox upon clicking):
function ff_copy_idbfFormName_master_init()
{
jQuery('[name="ff_nm_idbfCheckbox[]"]').click(function(){
var total = 0;
  jQuery('[name="ff_nm_idbfCheckbox[]"]:checked').each(function(){
    total += Number(jQuery(this).val());
});
  ff_getElementByName('idbfCheckboxTotal').value = total;
 });
} // ff_copy_idbfFormName_master_init

Now all works as needed. Every time a form user clicks any of the 6 radio boxes or items in the checkbox group, my hidden total fields update and provide the values needed for the Continue button to initiate the Status field to calculate a value of Good, Bad or Incomplete.

Please Log in or Create an account to join the conversation.

  • Page:
  • 1
Time to create page: 0.045 seconds

BreezingForms Pro 1.4.7 for WordPress Released!

Available in the membership section.

September Discount!

Massive discounts on all subscriptions!

Get Your Subscription Here

Quick Links

Downloads

BreezingForms

ContentBuilder

BreezingCommerce

Templates

Documentation

BreezingForms

ContentBuilder

BreezingCommerce

Apprendre BreezingForms (French Community)

Apprendre et maîtriser BreezingForms par des tutoriels et exemples, le tout en français

breezingforms.eddy-vh.com

Questions et réponses sur les forums de l'AFUJ

AFUJ

Special Offer

Summer Sale! All subscriptions at a special price!

Includes prio support, all of our current and future Joomla!® extensions and Joomla!® templates for the duration of your membership.

Get it from here

3rd Party Discount - 25% Off

We help you to keep your costs under control. If you are a new member and purchased a form building tool from a different form vendor, then you'll get a 25% discount on our subscription plans.

How to receive the discount:

Send us a quick email to sales@crosstec.org with a proof of purchase (for example a paypal receipt), await payment instructions and enjoy your membership!