Accessing Form Data via PHP

October 18th, 2014

If you want to programatically access a form’s data, follow this example code.

1
2
3
4
5
6
7
// Email all the Mike's
require_once(ABSPATH . 'wp-content/plugins/contact-form-7-to-database-extension/CFDBFormIterator.php');
$exp = new CFDBFormIterator();
$exp->export('my-form', array('show' => 'name,email', 'search' => 'Mike'));
while ($row = $exp->nextRow()) {
    wp_mail($row['email'], 'Hello ' . $row['name'], 'How are you doing?');
}

NOTES:

  1. The second argument to “export” (the array) is optional. But use it to set options just as you would for shortcodes. The example above shows how to use the ‘search’ shortcode option to limit entries (rows) to only those that have “mike” in them.
  2. Be sure to iterate over all of the data event if you do not use it (i.e. don’t ‘break’ out of the while loop.). This is related to the fact that mysql_unbuffered_query call is being used behind the scenes.
  1. Greg
    April 6th, 2011 at 21:28 | #1

    Is this the easiest way to add form data to a page template?

  2. msimpson
    April 7th, 2011 at 13:51 | #2

    try using a shortcode

  3. Paul
    June 21st, 2011 at 06:10 | #3

    Apologies if it’s a stupid question but where do I place this piece of code to allow contact form 7 results to be queried?

  4. Michael Simpson
    June 21st, 2011 at 10:53 | #4

    @Paul
    Most likely in a custom page template where you can include your own php

  5. Michael Simpson
    June 21st, 2011 at 13:11 | #5

    @Paul
    ..and come to think of it, there is a plugin called Shortcode Exec PHP where you can create your own shortcode in an admin page. You put in a piece of PHP code to execute. That might be easier to use.
    http://wordpress.org/extend/plugins/shortcode-exec-php/

    For example, I was able to use it like this:
    require_once(ABSPATH . 'wp-content/plugins/contact-form-7-to-database-extension/CFDBFormIterator.php');
    $exp = new CFDBFormIterator();
    $exp->export('Contact form 1', array());
    while ($row = $exp->nextRow()) {
    echo 'Submitted=' . $row['Submitted'] . '<br/>';
    }

  6. Michael Simpson
    June 21st, 2011 at 15:54 | #6

    @Michael Simpson
    I created this page to better document this idea: http://cfdbplugin.com/?page_id=444

  7. Ron D.
    November 4th, 2011 at 05:28 | #7

    I would like to display the image instead of the link. how can I do this, please advise.

    • Michael Simpson
      November 4th, 2011 at 09:24 | #8

      Good question. You would need to access the API to get the URL of an uploaded image file.
      I haven’t tested this code, but something like this:

      // Set these variables to your form
      $formName = 'Contact form 1';
      $fieldNameWithFile = 'upload';

      require_once(ABSPATH . 'wp-content/plugins/contact-form-7-to-database-extension/CF7DBPlugin.php');
      $plugin = new CF7DBPlugin();
      require_once(ABSPATH . 'wp-content/plugins/contact-form-7-to-database-extension/CFDBFormIterator.php');
      $exp = new CFDBFormIterator();
      $exp->export($formName, array());
      while ($row = $exp->nextRow()) {
          $fileUrl = $plugin->getFileUrl($row['submit_time'], $formName, $fieldNameWithFile);
          echo "<img src='$fileUrl'/> <br/>";
      }

  8. December 12th, 2011 at 23:40 | #9

    I have been working on a site that uses a lot of php to pull form data into pages to make it extremely easy to update by a lot of non-technical people and your cfdb plugin has been an enormous help, so thanks!

    I am trying to figure out something similar to Ron as well. I can submit an attachment via cf7 and can then view the attachment by clicking on it in the database but I cannot figure out where the image is being stored to be able to use php to pull it. Tried the code in the previous post and all we got was question mark where their should have been a pic. I have even tried looking through the wordpress files in my sites folder where all the rest of the data is and I cannot seem to find it.

    Any assistance would be appreciated!

    Thanks!

    • Michael Simpson
      December 13th, 2011 at 21:42 | #10

      I think I have an bug here in the code where the “submit_time” field is not being given returned rows. The work-around is to explicitly add it to “show”.

      I went through the exercise of creating my own short code and created this code for the short code:
      require_once(ABSPATH . 'wp-content/plugins/contact-form-7-to-database-extension/CFDBFormIterator.php');
      require_once(ABSPATH . 'wp-content/plugins/contact-form-7-to-database-extension/CF7DBPlugin.php');

      $exp = new CFDBFormIterator();
      $plugin = new CF7DBPlugin();

      // $atts is the array of short code attributes in Shortcode Exec PHP
      $formName = $atts['form'];
      $fieldName = $atts['field'];

      $exp->export($formName, $atts);
      while ($row = $exp->nextRow()) {
      $fileName = $row[$fieldName];
      if ($fileName) {
      $fileUrl = $plugin->getFileUrl($row['submit_time'], $formName, $fieldName);
      echo "";
      }
      }

      I called my short code “show_image” if I use this one it failed because of missing submit_time.

      [show_image form="Contact form 1" field="upload"]

      But if I used this one it worked because submit_time is explicitly called to be shown. (“upload” is the name of the field I have with images)
      [show_image form=”Contact form 1″ field=”upload” show=”submit_time,upload”]

      If you are not using a short code but coding this directly into a page template, then you would need to initialize $atts like this:
      $atts = array( 'form' => 'Contact form 1', 'field' => 'upload', 'show' => 'submit_time,upload');

      • Michael Simpson
        December 14th, 2011 at 10:21 | #11

        I changed some code to make this work and to be simpler. I made it so that if you have an ‘upload’ field for example, you will have a $row[‘upload_URL’] value to get that so you don’t need to call a function and import extra stuff to get it. I have not pushed this out, but I documented it at the end of the create your own short code page. If anyone needs this urgently, let me know and I’ll push out an update.

  9. shajo
    December 20th, 2011 at 04:17 | #12

    Hi Michael…..using shortcode is it possible to get data from database related to the logged in user(dynamically)? my client needs one option like mycompany for each users have their company information so i need to display companies according to the logged in user.is it possible to get the company information using shortcode?

  10. shane
    February 23rd, 2012 at 19:26 | #13

    Hi,
    Would you teach me how to use ‘select distinct’?

  11. March 14th, 2012 at 05:46 | #14

    @Michael Simpson
    i want to show uploaded file for admin with link how can i do this please help me out plssss plsss.

  12. March 14th, 2012 at 06:10 | #16

    @sushant
    its very urgent please do me a favourr please sir………

  13. Bayu W
    April 25th, 2012 at 11:44 | #17

    @Michael Simpson
    Hi Michael,
    Could we put this piece of code into a new PHP file?
    Since I got “Call to a member function get_results() on a non-object. … /plugins/contact-form-7-to-database-extension/ExportBase.php on line 344” error.

    I need to create an PHP file that produce query output.

    Thanks,
    [bayu]

  14. Michael Simpson
    April 25th, 2012 at 13:33 | #18

    @Bayu W
    I don’t understand what the error has to do with putting code into a different file. Which code & different from what?

  15. Bayu W
    April 26th, 2012 at 18:46 | #19

    @Michael Simpson
    I want to create a single new PHP file in my DocRoot, and put that kind of piece of code above to query my form.
    I guess I missed some required PHP files/classes, since I only put “require_once(ABSPATH . ‘wp-content/plugins/contact-form-7-to-database-extension/CFDBFormIterator.php’);”

    What WP classes should be loaded before.
    I’m a newbie on WP development.

  16. Bayu W
    April 26th, 2012 at 19:17 | #20

    @Michael Simpson
    For clarity, regarding your comment above:
    @Paul
    Most likely in a custom page template where you can include your own php

    Should we put it into template of Page or might create a new one?
    For now, we have on WP default template pages are: Default Template, Archive, Blog.

    I just to create a simple PHP file and accessed from i.e.: http://mysite.com/simplepage.php

    Thanks,
    [bayu]

  17. Michael Simpson
    April 26th, 2012 at 19:43 | #21

    @Bayu W
    I don’t think you can just put a file in the doc root and access it directly. You need to be in the “WP environment” where it has already loaded up lots of stuff like DB connection stuff that the plugin expects to be available. You could put this code in a page template PHP file.

  18. Bayu W
    April 26th, 2012 at 20:02 | #22

    @Michael Simpson
    Thanks Michael! 🙂

  19. Michael Simpson
    February 12th, 2013 at 10:45 | #23

    @Michael Simpson
    To include the “WP environment” you would need to include the wp-load.php file before you put any of the code from the example on this page.

    // Load WP environment
    require_once('wp-load.php');

    but fix the path to the file relative to the file you put this code in.

  20. Patrizia
    March 18th, 2013 at 07:05 | #24

    Hi, I’m showing the content of the DB in a php page; how can I re-order the posts according to a specific field?

    Thanks in advance
    Patrizia

    • Michael Simpson
      March 18th, 2013 at 10:03 | #25

      Standard short code options apply here. You can use “orderby”.
      Line 4 from the example could be written

      4
      
      $exp->export('my-form', array('show' => 'name,email', 'search' => 'Mike', 'orderby' => 'name'));
  21. Anjali Bhati
    April 7th, 2013 at 05:40 | #26

    Hi, I want to extract the hidden web documents from search pages through PHP code. How can i do?

    • Michael Simpson
      April 7th, 2013 at 08:36 | #27
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      
      require_once(ABSPATH . 'wp-content/plugins/contact-form-7-to-database-extension/CF7DBPlugin.php');
      require_once(ABSPATH . 'wp-content/plugins/contact-form-7-to-database-extension/CFDBFormIterator.php');
      $plugin = new CF7DBPlugin();
      $exp = new CFDBFormIterator();
      $formName = 'my-form';
      $fileFileName = 'uploaded-file';
      $exp->export($formName, array());
      while ($row = $exp->nextRow()) {
         $file = $this->getFileFromDB($row['submit_time'], $formName, $fileFileName);
         if ($file != null) {
            // the file name is $file[0]
            // the file contents are in $file[1]
         }
      }
  22. Anjali Bhati
    April 7th, 2013 at 10:24 | #28

    Thanks Sir,,,
    But I type this coddde on NOTEPAD-

    export($formName, array());
    while ($row = $exp->nextRow()) {
    $file = $this->getFileFromDB($row[‘submit_time’], $formName, $fileFileName);
    if ($file != null) {
    // the file name is $file[0]
    // the file contents are in $file[1]
    }
    }
    ?>
    and then RUN ON WAMP SERVER . It’s show the error—
    Notice: Use of undefined constant ABSPATH – assumed ‘ABSPATH’ in C:\wamp\www\PROJ PP\extract.php on line 2
    Call Stack

    Warning: require_once(ABSPATHwp-content/plugins/contact-form-7-to-database-extension/CF7DBPlugin.php): failed to open stream: No such file or directory in C:\wamp\www\PROJ PP\extract.php on line 2

    Fatal error: require_once(): Failed opening required ‘ABSPATHwp-content/plugins/contact-form-7-to-database-extension/CF7DBPlugin.php’ (include_path=’.;C:\php\pear’) in C:\wamp\www\PROJ PP\extract.php on line 2
    Please tell me full code,,,,,,,,From starting to end(

  23. Anjali Bhati
    April 7th, 2013 at 10:26 | #29

    export($formName, array());
    while ($row = $exp->nextRow()) {
    $file = $this->getFileFromDB($row[‘submit_time’], $formName, $fileFileName);
    if ($file != null) {
    // the file name is $file[0]
    // the file contents are in $file[1]
    }
    }
    ?>

  24. Michael Simpson
    April 7th, 2013 at 16:26 | #30

    @Anjali Bhati
    In this case you are not running in with the WordPress environment loaded up. You will need to “require_once” the wp-load.php file in your installation to get that set up. Put that line of code first.

  25. Anjali Bhati
    April 8th, 2013 at 03:40 | #31

    How can i in simple way?
    Can you tell,coding to extract hidden web documents from search pages that run on wamp server.

  26. Anjali Bhati
    April 8th, 2013 at 12:29 | #32

    Please Sir , tell me as soon as possible,,,,,,,,,,,,,,

    • Michael Simpson
      April 8th, 2013 at 18:14 | #33

      I suggest you simply use the short code [cfdb-html] with the option filelinks=”url”
      [cfdb-html form="myform" filelinks="url"] … add ${fields} here [/cfdb-html]
      That way links will be created for you.

  27. Anjali Bhati
    April 8th, 2013 at 23:35 | #34

    Sir, i am learning php so thats why i don’t know much more. This is our lab experiment. Our 50 marks is based on this experiment.
    Please Sir, tell me full code from starting to end( ).

  28. Anjali Bhati
    April 9th, 2013 at 07:19 | #35

    Sir, tell me code for extract the attributes and values from search pages which run on WAMP server,,,,,,,,,,,,starting to end.

    as soon as possible,,,,,,,,,,,,,,,,,,,,,,,

  29. Anjali Bhati
    April 9th, 2013 at 07:20 | #36

    Sir tell me as soon as possible,,,,,,,,,,,,,,,,,,,

  30. Michael Simpson
    April 9th, 2013 at 16:22 | #37

    @Anjali Bhati
    My last suggestion is to use a short code in a post. No PHP is needed. That should allow you to show the information from the form submissions on a page. It will include download links. I don’t know why you want to do this in PHP. My guess is that you do not know much about how this plugin works. I think you found this page but you do not know enough to understand that this page is not the solution you need. I am guessing that what you need is much simpler. Try using the [cfdb-html] short code. Otherwise explain to me why the using the short code is not sufficient. I will give you code suggestions if you need them, but I will not code the entire solution for you.

  31. brodster
    April 28th, 2013 at 00:46 | #38

    how would i display the pager at the bottom?

    • Michael Simpson
      April 29th, 2013 at 10:32 | #39

      I assume you are talking about using [cfdb-datatable]. See “Why doesn’t my data table look like the one in the Admin Panel” on http://cfdbplugin.com/?page_id=91

Comments are closed.  Go To Support Forum