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 populate some hidden fields via javascript then submit?

  1. gregdickson12

    This may not be the best way to go about things but I am trying to code up a simple tax calculator via gravity forms.

    I have a standard gravityform that records the input values, etc, records the entries and sends appropriate confirmation/thank you emails but then the actual calculation is done manually offline and the customer sent another email with the results.

    What i want to be able to do is add the simple calculation logic in some automatic way so that I can cut out the manual process completely.

    Rather than code all the form logic manually (i.e. drop gravity completely) and loose all the cool validation, processing, recording and auto emailing that gravity gives me I came upon the idea of adding some javascript that does the calculation on the client before the post is sent and adds the the result to a hidden gravity input field.

    Is this possible?

    I have copied the source produced by the normal gravityform page into a custom theme template and reproduced the original form page's behaviour (appreciate this is brittle and means the auto gen'd ids could get broken and have to be manually "aligned") but when i try and intercept the submit action with my own javascript or indeed provide my own href based fake submit button and then later call document.forms["gform_6"].submit(); the form doesnt seem to submit properly.

    Is this the correct/appropriate way to achieve my use case?

    I am presuming that a quick javascript form update would be best.

    Is there another way to add some custom logic to the processing flow such that the form gets the calculated value?

    I also want to display the form's submitted values back to the user on the confirmation page, I am presuming this is possible using some standard technique?

    Thanks for your patience with a java programmer using wordpress and php for the first time!


    Posted 12 years ago on Sunday October 31, 2010 | Permalink
  2. gregdickson12

    Thinking i should have read more about hooks and filters?

    The following hooks and filters allow developers to integrate Gravity Forms with other plugins.


    This action hook runs during form submission after validation has taken place and before entry data has been saved.

    Is this the way to go?

    I need to only run this hook for this one form and i need to set the hidden field values on the post vars. Will play but would appreciate wiser minds advice / guidance.


    Posted 12 years ago on Sunday October 31, 2010 | Permalink
  3. Yes, you could use the pre submission hook and do your calculation then and then populate the value of the hidden field so that it is then stored.

    Posted 12 years ago on Monday November 1, 2010 | Permalink
  4. Hi Carl,

    How would you go about doing that?

    I have created a hidden field which i wish to populate via the pre_submission hook action.
    I have enabled the option " Allow field to be populated dynamically" and have given the parameter name "ordernumber".

    say if for arguments sake i wish to add "TRA3232" value into the hidden field via the hooks action, would it be applied something like this below?

    add_action("gform_pre_submission", "pre_submission_handler");
    function pre_submission_handler($form_meta){
       $value = $form_meta['ordernumber'];
       $value = 'TRA3232';

    Is this how it would be applied ? Thanks :) Help or hints will be appreciated

    Posted 12 years ago on Monday November 29, 2010 | Permalink
  5. If all you rare doing is pre-populating a hidden field and not doing anything overly complex you can use the gform_field_value hook to populate a hidden field.

    In your case it would look like this:

    add_filter("gform_field_value_ordernumber", "populate_ordernumber");
    function populate_ordernumber($value){
    return "TRA3232";
    Posted 12 years ago on Monday November 29, 2010 | Permalink