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:
- 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.
- 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.
Is this the easiest way to add form data to a page template?
try using a shortcode
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?
@Paul
Most likely in a custom page template where you can include your own php
@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/>';
}
@Michael Simpson
I created this page to better document this idea: http://cfdbplugin.com/?page_id=444
I would like to display the image instead of the link. how can I do this, please advise.
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/>";
}
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!
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');
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.
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?
Hi,
Would you teach me how to use ‘select distinct’?
@Michael Simpson
i want to show uploaded file for admin with link how can i do this please help me out plssss plsss.
Look at the section “Links to uploaded files” on the Create Your Own Short Code page. You should be able to use that.
@sushant
its very urgent please do me a favourr please sir………
@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]
@Bayu W
I don’t understand what the error has to do with putting code into a different file. Which code & different from what?
@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.
@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]
@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.
@Michael Simpson
Thanks Michael! 🙂
@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.
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
Standard short code options apply here. You can use “orderby”.
Line 4 from the example could be written
Hi, I want to extract the hidden web documents from search pages through PHP code. How can i do?
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(
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]
}
}
?>
@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.
How can i in simple way?
Can you tell,coding to extract hidden web documents from search pages that run on wamp server.
Please Sir , tell me as soon as possible,,,,,,,,,,,,,,
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.
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( ).
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,,,,,,,,,,,,,,,,,,,,,,,
Sir tell me as soon as possible,,,,,,,,,,,,,,,,,,,
@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.
how would i display the pager at the bottom?
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