PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Dynamic population of fields by 2 variables: current date and quantity field.

  1. I searched (and couldn't really find an answer), so I figured it couldn't hurt to ask. For the record, I own a Developer's version of Gravity Forms, so all bells and whistles are available to me :) I LOVE Gravity Forms. I used to roll my own forms within WordPress because every plugin I tried was icky and not as good as mine - but this takes that cake :)

    Anyway, kissing up aside, I have what is probably the most complicated form I've ever done with Gravity Forms. (I could do it myself with rolling my own - but I bought Gravity Forms so I wouldn't have to do that anymore!) Most of it's pretty straightforward, but it's being used for a paid-for membership directory. I'm compiling the registration for right now (integrated with the PayPal Add-on) but I've run into two issues that I can't sort out.

    Please keep in mind that I'm REALLY good at messin' with WordPress and making it bow to my every whim. However, this will be the first time I've ever attempted to do any sort of coding that messes with Gravity Forms, and I'm not 100% sure how to accomplish what I need. So if anyone can point me in the right direction, it would be awesome. I'm not afraid fo getting my hands dirty, I just need to find the tools to use. (of course, if you want to supply some code examples, I would be forever in your debt!)

    So I need 2 things:

    First, I have a field that has a quantity option. What I would like is for a dynamic single-line text field to appear based on the number placed in the quantity field. So if "3" is put in for quantity, I'd like 3 single-line text fileds to appear.

    Next (and finally) I have a dropdown field that has membership level choices. There are 2 membership levels that are marked down to less than half-price if the date is between June 1 and August 15. (basically, it's a membership for someone who wants to join on the tail end of the previous year - they get a seriously discounted rate because they've missed most of the year) I'm trying to sort out how I can have my 2 membership levels "go away" and be replaced by these reduced-price fields between June 1 and August 15. (it would even be fine if I had to do two dropdowns, one with the regular prices and one with reduced prices and just show the reduced price version between those dates, and the regular priced one any other time.)

    So, anyone have ideas/suggestions for me? Or is more butt-kissing in order? (I'm a total believer in that whole "catch more flies with honey" thing.. XD)

    Thanks in advance!

    Posted 13 years ago on Tuesday December 20, 2011 | Permalink
  2. Okay, well I got one part working anyway - the date-based part. I just created two sets of fields - one for the regular price and one for the reduced price. Set classes for them ("full" and "reduced"). Since none of them are *required* all I had to do was write up a little function in my theme's functions.php file that checked the UNIX timestamp of today and compared them with the dates I want. If it's betweenJune 1 and August 15, it just hides the "full price" options, and displays only the reduced price stuff. On any other date, it'll show the full price and hide the reduced price.

    So that works! Now I just need to figure out the thing with the "creating X number of input fileds where X = whatever number is in a previous field" thing.

    Thanks!

    Posted 13 years ago on Tuesday December 20, 2011 | Permalink
  3. You can't modify a form dynamically based on an input like that. You would have to create the fields in the form, then hide them with conditional logic. Then you could have radio buttons with the numbers there, or a drop down, and then use conditional logic to unhide the proper number of additional text fields. The problem with this approach is that it's not scalable. And there are a lot of extra fields in your form. If you have numbers from 1 to 9, it's not too bad. If you're numbers are larger or unknown, it might not work.

    I think you might be able to work around it, but adding fields to a form based on a number input it not built into Gravity Forms.

    Posted 13 years ago on Tuesday December 20, 2011 | Permalink
  4. Okay. Yeah, that won't work very well. What about having a "button" added so you can "add another field" on the fly, using jQuery (example: http://charlie.griefer.com/blog/jquery-dynamically-adding-form-elements/)? I see Gravity Forms doesn't do this now, but if I add a button with HTML input, will Gravity Forms pass the information?

    Posted 13 years ago on Tuesday December 20, 2011 | Permalink
  5. No, Gravity Forms will not know about that field and will not process it.

    Posted 13 years ago on Wednesday December 21, 2011 | Permalink
  6. Well I guess I know what I'm putting in as a "feature request" then :)

    ETA: the suggestion above (creating fields and having them show using conditional logic based on what number is chosen in the dropdown) won't work, since I have variable conditions. The "how many" dropdown is shown only if a previous choice is made - so when I start adding the extra fields, the fields all show, because I can't do a conditional statement along the lines of "Show this field IF choice #1 is made AND when choice #2 is made OR choice #3 is made" So that won't fly.

    I guess I'll keep digging!

    Posted 13 years ago on Wednesday December 21, 2011 | Permalink
  7. Chris or Shelly, where can I get a copy of the function you wrote to hide/display the full/reduced fee options based on date range. That's exactly what I'm trying to do. Thanks.

    Posted 12 years ago on Thursday July 12, 2012 | Permalink
  8. JE, can you please explain what you're looking to do? Start a new topic if it's appropriate and we'll be happy to provide you personal assistance for your current issue. I can't remember what Shelly came up with but she accomplished most of it on her own.

    Thanks,
    Chris

    Posted 12 years ago on Friday July 13, 2012 | Permalink
  9. Like Shelly, I need to present one set of radio button options/prices up to a certain date, and a different set of radio button options/prices after a certain date, and I want to get my hands on the functions.php function she wrote to accomplish that because I have not figured it out mystelf.

    Posted 12 years ago on Friday July 13, 2012 | Permalink
  10. Her website is in her profile if you want to try to contact her:
    http://brassblogs.com

    She might not check back in on this topic although she has been active in the past month.

    Posted 12 years ago on Friday July 13, 2012 | Permalink
  11. Hey JEWebDes, sorry I haven't checked back in a while. Seems I only come around when I need something ;) I think you *did* contact me - if it wasn't you, someone else emailed me and asked the same question, but to help others out that may have the same question, what I did in this instance was create 2 forms, and used PHP conditional logic to check the current date. If today's date was within the "reduced rate" date range, it would show form #2, but if it was any other time, it would show form #1.

    However, since then I've discovered more goodies with the stuff you can do with Gravity Forms, and if I had to do it over again, I'm pretty sure I could make it happen with just one form. A little while back, I had a similar question (regarding dynamic authors, instead of dates), and that forum thread is here: http://www.gravityhelp.com/forums/topic/post-authors. Knowing all of that info, I'm 100% sure I could, instead, create a single form, then write a function that would look at that one field and swap it out based on the date. Would be MUCH easier to maintain, since you only have to mess with one form instead of two.

    Hope that helps someone else!

    Posted 12 years ago on Thursday August 2, 2012 | Permalink
  12. Okay, I wanted to come back and update this, in case it would help anyone else. I edited the form to basically show/hide (and require) a field based on the date range. I'm gonna pop in a lot of code here, but hopefully it helps someone in the future. it uses the stuff I mentioned in the above post.

    So you create one single form. let's say, for example, that you have a membership fee that changes based on the date. i.e. at the beginning of the year, it's full price, but when the year is half gone (say, in July) the price is reduced by 50% because the year's half over.

    SO what I did was create a single form, and in it (for brevity's sake, we'll do one field, but there were several in my form) there's a "membership fee". Dropdown or quantity field, it doesn't matter. You just set it as "required", and set the CSS Class as "fullyear".

    Duplicate the field. Change the price. Set the cssClass as "midyear".

    Now, just add this function to your functions.php file:

    // function to convert date to UNIX timestamp
    function date_comparison_fun($date='') {
        if(!$date) $date = date('j F Y'); 			// default - today's date
    	  $date = strtotime($date); 			// UNIX conversions
        return $date;
    }
    
    //Now use the above to change the form fields based on date
    add_filter('gform_pre_render', 'change_form_stuff', 10, 2);
    function change_form_stuff($form) {
      // dates MUST be entered in day Month Year format. Example: 20 September 2012
      // blank date is today's date.
      $today = date_comparison_fun();
      $year  = date('Y');                       // current year
      $start = date_comparison_fun('30 June ' . $year);
      $end   = date_comparison_fun('31 December ' . $year); 
    
      if($form['id'] == 1) {                      // form ID you want to use this in
        //start by changing the form title
        if( ($today > $start) && ($today < $end) )
          //if today is between start and end dates, it's Mid-Year stuff
          $form['title'] = $form['title'] . ' - Mid-Year Membership';
    
          //now get to the meat - the fields
          foreach($form['fields'] as &$field) {
            if( ($today > $start) && ($today < $end) ) { // if we're in between the dates
              if($field['cssClass'] == 'fullyear') {     //full year pricing is gone
                $field['isRequired'] = false;
                $field['cssClass'] = $field['cssClass'] . ' hide';
              }
            } else {                                    // else for the rest of the year
              if($field['cssClass'] == 'midyear') {     // mid-year pricing is gone
                $field['isRequired'] = false;
                $field['cssClass'] = $field['cssClass'] . ' hide';
              }
            } //end date check
          } // end foreach
        } //end checking which forms
    
      return $form;
    }

    What the above does is, if it's before July 1, it'll show the "full year" memebership; and set the "midyear" field as 1) not required and 2) add an additional class of "hide". if it's after July 1, but before December 31, it'll do the opposite - fullyear is now not required and hidden, and the midyear *is* required and shown.

    the final step is to add this to your style.css file in your theme:

    .hide {
    display:none !important;
    }

    Hope that helps someone out :)

    Posted 12 years ago on Thursday September 6, 2012 | Permalink