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.

perform a calculation and change values being submitted.

  1. jorges
    Member

    Hi,

    I'm trying to take the values entered in two fields in a given form and perform a calculation with them before storing the result on the database. I'm also trying to set the two field's values to 0 before storing the result.

    Currently I'm using the pre_submission_handler hook and I can access my values and calculate them.

    What I'm stuck with is how do I then go about setting the values that are going to be submitted to the database to whatever I want.

    Being a complete newbie at php I managed to somehow get to this:

    add_action("gform_pre_submission", "pre_submission_handler");
    function pre_submission_handler($form_meta){
    	$calculation = (($_POST["input_2"] / $_POST["input_1"]) - 1) * 100;
    	$_POST["input_3"] = $calculation;
    	echo $_POST["input_3"];
    }

    This displays the right calculation but does not store it on the database.
    Any help would be greatly appreciated.

    Posted 14 years ago on Monday March 1, 2010 | Permalink
  2. jorges
    Member

    bump

    Posted 14 years ago on Tuesday March 2, 2010 | Permalink
  3. jorges
    Member

    It would be nice if someone would tell me if I'm wasting my time here or will this request be eventually looked at.

    Posted 14 years ago on Tuesday March 2, 2010 | Permalink
  4. We'll get our lead developer to take a look at this one and offer a suggestion. We're always busy with other support requests as well so please be patient and we'll get back to you as soon as possible with something.

    Posted 14 years ago on Tuesday March 2, 2010 | Permalink
  5. jorges
    Member

    That would be very helpful. Many thanks.

    Posted 14 years ago on Tuesday March 2, 2010 | Permalink
  6. jorges,
    You were close. Only needed to declare the $_POST variable as global. Try the following:

    add_action("gform_pre_submission", "pre_submission_handler");
    function pre_submission_handler($form_meta){
        global $_POST;
    
        $calculation = (($_POST["input_2"] / $_POST["input_1"]) - 1) * 100;
        $_POST["input_3"] = $calculation;
        echo $_POST["input_3"];
    }
    Posted 14 years ago on Tuesday March 2, 2010 | Permalink
  7. jorges
    Member

    DOH!

    What a beginners mistake...

    Got another question on how I can make sure this only runs for one form but I'll open another topic for that.

    Many thanks for your help, the plugin is everything I wanted and more and I'll be sure to recommend it.

    Posted 14 years ago on Wednesday March 3, 2010 | Permalink
  8. jorges
    Member

    Cracked it. I'm posting the code in case another user wants to do something similar. With some modification this can be used for scoring surveys, etc.

    add_action("gform_pre_submission", "pre_submission_handler");
    function pre_submission_handler($form_meta){
        global $_POST;
    
        if($form_meta["id"] != '1'){
            return;
        }
    
        $calculation = (($_POST["input_2"] / $_POST["input_1"]) - 1) * 100;
        $_POST["input_7"] = $calculation;
    	$_POST["input_1"] = '0';
    	$_POST["input_2"] = '0';
    }

    Thanks again for your help.

    Posted 14 years ago on Wednesday March 3, 2010 | Permalink
  9. Very nice! Glad you figured it out.

    Posted 14 years ago on Wednesday March 3, 2010 | Permalink
  10. How would you (or is there a way to) identify the _POST values by name rather than input_7 / etc?

    I'd like to use something similar to generate a password string via PHP and add it into a hidden field and have it display in the email notification. Atleast until the feature for user generation is in place :)

    Posted 14 years ago on Friday March 12, 2010 | Permalink

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