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.

gform_after_submission - writing to a MySQL database

  1. Hi- I would like to take my form submission and write certain fields to a database. The fields I would need to get into variable form so I can write to the database would be name, email, website. The code below is what I found to get me started, but I don't know how to create the variables which will pull the associated fields I need.

    I have the code to write to the database, I just need to pull those variables from the form submission. If anyone can help, I'd really appreciate it.

    Thanks,
    Bryan

    <?php
    add_action('gform_after_submission', 'post_to_third_party', 10, 2);
    function post_to_third_party($entry, $form) {

    // Need to create variables with the form values name/email/website

    }
    ?>

    Posted 10 years ago on Monday June 10, 2013 | Permalink
  2. andrew carbn
    Member

    Hey,

    I'm also trying to do exactly the same thing. I have a bit more code BUT IT ISN'T WORKING FOR ME.

    <?php
    add_action("gform_after_submission_9", "push_fields", 10, 2);
    function push_fields($entry, $form){

    $uploaderName = $entry["1"];
    $organiserName = $entry["2"];
    $organiserEmail = $entry["3"];
    $organiserNumber = $entry["4"];
    $venueNumber = $entry["5"];

    $con=mysqli_connect("hostname","username","password","dbname");
    mysqli_query($con,"INSERT INTO table (uploaderName, organiserName,
    organiserEmail, organiserNumber, venueNumber) VALUES ('$uploaderName',
    '$organiserName', '$organiserEmail', '$organiserNumber', '$venueNumber')");
    }
    ?>

    The Entry Object is detailed here:
    http://www.gravityhelp.com/documentation/page/Entry_Object

    Hope this gives you some ideas. The code above shows no errors but also has no effect.

    Posted 10 years ago on Tuesday June 11, 2013 | Permalink
  3. Andrew- I did get my code to work and send me an email (test) after submission. Make sure you put this code in your functions.php file of your theme. I'm on a friend's computer now (mine is in the Apple shop). Later this week when I get it back (hopefully tomorrow), if you are still having issues, I'll write to a dB to test the code.

    Does your PHP code write to a database if executed on it's own?

    Bryan

    Posted 10 years ago on Tuesday June 11, 2013 | Permalink
  4. andrew carbn
    Member

    Hi Bryan thanks for the quick reply.

    Yes the mySQL query does execute correctly by its self.

    I tried adding the code to functions.php but it kept crashing my site with an error code saying add_action... was undefined. So I removed just that line and that stopped it from crashing. I then added the add_action line into the php of the page, just above the form short-code, but again nothing was happening.

    I'm new to PHP and mySQL but I'm reasonably sure the problem is that the hook is not firing at all. Is there a way to check?

    Posted 10 years ago on Tuesday June 11, 2013 | Permalink
  5. David Peralty

    You shouldn't have received that error message. You are adding this code to the functions.php file for your theme, correct? In your wp-content/themes/selectedtheme/functions.php file?

    Posted 10 years ago on Tuesday June 11, 2013 | Permalink
  6. David Peralty

    You shouldn't have received that error message. You are adding this code to the functions.php file for your theme, correct? In your wp-content/themes/selectedtheme/functions.php file?

    Posted 10 years ago on Tuesday June 11, 2013 | Permalink
  7. andrew carbn
    Member

    Thanks David! By assuming I am an idiot, you have helped me to realise I am an idiot! :-)

    I didn't realise there is more than one functions.php file!

    It's all working now. Couldn't be happier right now.

    Thanks loads for your help.

    Posted 10 years ago on Tuesday June 11, 2013 | Permalink
  8. David Peralty

    Not assuming anything is how I helped ;) Glad you got it sorted.

    Posted 10 years ago on Wednesday June 12, 2013 | Permalink