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.

How to get form id?

  1. Hi,
    I'm trying to create a function which will add a field to the user metadata table on form submission. I'd like that field to contain an array of all the fields/responses in the submitted form.

    I tried using the gform_post_submission action hook and setting $form_contents = $entry['field_id'] as described on this page: http://www.gravityhelp.com/documentation/page/Entry_Object

    But no go. Any ideas on how to get this working? I suspect I'm trying to get the form/field contents in the wrong way. Here's my current function:

    add_action("gform_post_submission", "set_additional_user_fields");
    function set_additional_user_fields($user_id) {
        $form_contents = $entry['field_id'];
        add_user_meta($user_id, 'form_info', $form_contents);
    }

    Thanks for your help!

    Posted 12 years ago on Wednesday August 10, 2011 | Permalink
  2. Hi Michelle,

    Should the entry data be added to the user submitting the form or a specific user? The gform_post_submission hook passes the $entry variable (documentation here). If the submitting user is logged in at the time of the submission their user ID will be included in the $entry in the created_by property. Here is a start:

    http://pastie.org/2352056

    Posted 12 years ago on Wednesday August 10, 2011 | Permalink
  3. Thanks David!! That works great for getting a specific field value from the form.

    Do you know, is there any way to retrieve all the field name/value pairs in an array? I thought field_id would do the trick based on this from the entry_object page:

    FIELD_ID
    (string) The value for all submitted fields can be retrieved by using the Field Id as the key to the $entry array.

    But maybe not? Thanks again,

    Michelle

    Posted 12 years ago on Wednesday August 10, 2011 | Permalink
  4. Hi Michelle,

    The FIELD_ID is actually not a property of the entry object. It is listed to imply that using the field ID you can get the submitted value of the desired field (ie $entry[10]). Here is a some code that will retrieve the submitted values from the entry and aggregate them into an array (titled $submitted_info in the snippet). You can combine this code with the previously provided code.

    http://pastie.org/2352331

    Posted 12 years ago on Wednesday August 10, 2011 | Permalink
  5. You rock!! Thank you!

    Unfortunately I had to hack a bit of the core code to get everything working just the way I needed it to, but good news is, I think I now have a way to link my saved form to a logged-in user's profile (so the user can come back and edit the form / resubmit later). In any case this is working for me. :-)

    Here's what I had to do to achieve it:

    UPDATE - see this post for a revised solution:
    http://www.gravityhelp.com/forums/topic/saving-a-form#post-32318

    1. In your form, go into each field's Advanced tab and click "Allow Field to be Populated Dynamically. Enter a parameter name - it can be anything, but should be unique.
    2. Change line 909 of forms_model.php to read:
      return apply_filters("gform_field_value_$name", apply_filters("gform_field_value", $value, $name));
    3. Add the following to your theme's functions.php file:
      // RETRIEVE PARAMETER_NAME FOR FIELDS AND ADD TO WP_USERMETA
      add_action('gform_post_submission', 'save_entry_values', 10, 2);
      function save_entry_values($entry, $form){
          $userid = $entry['created_by']; // retrieve user ID
          $submitted_info = array();
      
          foreach($form['fields'] as $field) {
      
              $value = RGFormsModel::get_lead_field_value($entry, $field);
      
              if($value) {
                  $submitted_info[] = array($field['inputName'] => $value); // retrieves parameter_names values
                  update_user_meta($userid, $field['inputName'], $value); // stores parameter_name values in wp_usermeta table
              }
          }
      }
      
      // RUN REVISED GFORM_FIELD_VALUE_YOUR_PARAMETER FILTER
      add_filter('gform_field_value', 'populate_user_meta', 10, 2);
      function populate_user_meta($value, $name){
          global $user_ID;
          $field_user_meta = get_the_author_meta($name, $user_ID);
          return $field_user_meta;
      }
    4. Now when logged-in users fill out a form, they should be able to return to that same form later and have all their previous answers pre-populated.

    The way the filter is rewritten should allow users to use both the gform_field_value filter and the original gform_field_value_your_parameter filter - the latter will overwrite the former.

    Let me know if you can think of a better way to do this - any of it - and/or if there's a way I can move that rewritten filter into my local functions.php file. I hate to edit the core code, just couldn't see a way around it in this instance.

    Thanks again for your help!

    Michelle

    Posted 12 years ago on Wednesday August 10, 2011 | Permalink
  6. UPDATE:

    Michelle has posted revised code here:
    http://www.gravityhelp.com/forums/topic/saving-a-form#post-32318

    Posted 12 years ago on Thursday August 11, 2011 | Permalink

This topic has been resolved and has been closed to new replies.