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.

Grabbing $_POST to simulanteously store in a second database

  1. I have the user registration add-on setup and its working great. However I have a separate affiliate script running outside of the confines of wordpress. When a user fills out the gravity form and is added to the WP database I need to be able to grab the $_POST entry variables and store them in a separate database for the affiliate script. I realize this is custom, but I need some guidance on gform_post_submission

    I think after reading the forums that it should be pretty easy to do, but I can't seem to figure out gform_post_submission. I see that each $_POST is actually stored in and entry array. Is it as simple as creating a basic MySQL function to store each $entry[]; submission in my second database?

    Posted 12 years ago on Sunday July 31, 2011 | Permalink
  2. You have to use the gform_post_submission hook which has access to the Entry Object which contains the data. You'd then have to write custom PHP to read the values you want from the Entry object and insert them into your database table.

    Here is documentation on the gform_post_submission hook:
    http://www.gravityhelp.com/documentation/page/Gform_post_submission

    Here is documentation on the entry object:
    http://www.gravityhelp.com/documentation/page/Entry_Object

    So this requires knowledge of PHP and hook usage to be able to implement it properly. You want to use the gform_post_submission hook because this is going to give you access to the data AFTER form validation has taken place.

    Posted 12 years ago on Monday August 1, 2011 | Permalink
  3. Thanks -

    I realize this requires custom PHP...that is not a problem. I am just having a problem identifying exactly how gform_post_submission works.

    I have placed the following in my functions.php file. I know this is not correct, but I am looking to get put on the right track.

    http://www.pastie.org/2306344

    Thanks

    Posted 12 years ago on Monday August 1, 2011 | Permalink
  4. Couple things to make this work.

    This line:

    add_action("gform_post_submission_1", "post_submission", 10, 2);

    You need to change the "post_submission" there to the name of your function. You're hooking YOUR function to the "gform_post_submission" action. Since the name of your function is store_in_db2 the line above should look like this:

    add_action("gform_post_submission_1", "store_in_db2", 10, 2);

    Otherwise, your function will never run.

    Also in that line: this hook will be run for form #1 only (that's the _1 added to the end of the hook name.) Be sure the ID of your form is 1. If not, change this number to match your form.

    Finally, in the same line, you're saying that 2 arguments are accepted (that's what the 2 at the end is.) So, you should call your function with 2 arguments. This line:

    function store_in_db2($entry){

    Should look like this:

    function store_in_db2($entry, $form){

    After that, you will be able to get all the values from your form submission. You can determine the field id to use by looking at the source of a rendered page. For the name field, if you're using a name field, the ID might look like this:

    <input type='text' name='input_1.3' id='input_12_1_3' value='' tabindex='1' />
    <input type='text' name='input_1.6' id='input_12_1_6' value='' tabindex='2' />

    In that case, the values you want are 1.3 and 1.6. Maybe it's not clear from this example that input_12_1_3 becomes 1.3, but if you dump the $entry array, you will see this:

    [1.3] => Chris2
       [1.6] => Hajer2
       [2.1] => 1234 Maple
       [2.3] => Berwyn
       [2.4] => IL
       [2.5] => 60402
       [2.6] => United States
       [3] => this is my awesome comment

    That is part of a submission I made with this form:
    http://gravity.chrishajer.com/testing-the-post/

    If you look at the source of the page, you will see the input_12_1_3 and input_12_1_6 I referenced above, and how they correlate to the [1.3] and [1.6] in the $entry object.

    In your MySQL query line, if your form were identical to mine, you would do this: [EDITED 9/2/2011]

    [php]
    $query = 'INSERT INTO affiliates (FirstName, LastName) VALUES (\''. $entry['1.3'] . '\', \'' . $entry['1.6'] . '\')';
    mysql_query($query);

    For me, that resulted in this query being generated:

    INSERT INTO affiliates (FirstName, LastName) VALUES ('Chris2', 'Hajer2')

    Replace the 1.3 and 1.6 with the id of the form fields from your form and you'll be good to go. Please post if you need more assistance, or if this works out for you. Thanks.

    Posted 12 years ago on Tuesday August 2, 2011 | Permalink
  5. I think I'm missing something.

    This line works perfectly and puts a new record into the table:

    mysql_query("INSERT INTO gns_registration (fname) VALUES ('testvalue')", $con);

    (and I started to cheer and say, "Hey that made my long weekend; I can relax")
    but this one:

    mysql_query("INSERT INTO gns_registration (fname) VALUES ($entry['1']", $con);

    generates an error message:

    Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING'

    (and that made me sad)

    Have I misunderstood or am I typing something really wrong?

    Lawrence

    Posted 12 years ago on Friday September 2, 2011 | Permalink
  6. Looks like in my example I forgot to single quote the $entry['1'], so it's passed as unquoted to mysql_query, which won't work.

    Try building the query separately, then pass it to mysql_query:

    [php]
    $query = 'INSERT INTO gns_registration (fname) VALUES (\'' . $entry["1"] . '\')';
    mysql_query($query, $con);

    Sorry about the earlier syntax error. I will edit that now.

    Posted 12 years ago on Friday September 2, 2011 | Permalink
  7. Chris:

    Thank-you! Thank-you!

    I will try this out and get back to you!

    Lawrence

    Posted 12 years ago on Saturday September 3, 2011 | Permalink