{"id":377,"date":"2011-04-01T14:26:36","date_gmt":"2011-04-01T18:26:36","guid":{"rendered":"http:\/\/cfdbplugin.com\/?page_id=377"},"modified":"2015-08-26T08:14:00","modified_gmt":"2015-08-26T12:14:00","slug":"submitting-data-via-php","status":"publish","type":"page","link":"https:\/\/cfdbplugin.com\/?page_id=377","title":{"rendered":"Submitting Data via PHP"},"content":{"rendered":"<p>Perhaps you wish to save data from a form of your own, not CF7 or FSCF. Or perhaps you just want to programatically push data into the DB. To do this there are two basic things you will need to know:<\/p>\n<ol>\n<li>How to struture your form data so that the plugin knows how to consume it<\/li>\n<li>How to call the plugin&#8217;s save data function<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h2>Structuring the data<\/h2>\n<p>Data should be\u00a0structured\u00a0like this:<\/p>\n<pre><code>$data = (object) \u00a0array(\r\n    'title' =&gt; 'form-name',\r\n\u00a0\u00a0 \u00a0'posted_data' =&gt; array(\r\n    'fname' =&gt; $_POST['fname'],\r\n    'lname' =&gt; $_POST['lname'],\r\n    'email' =&gt; $_POST['email']),\r\n    'uploaded_files' =&gt; null);\r\n<\/code><\/pre>\n<p>Where &#8216;form-name&#8217; is the name of the form, and &#8216;fname&#8217;, &#8216;lname&#8217;, and &#8217;email&#8217; are form fields in this example. Replace them with the fields from your form.<\/p>\n<p><strong>Uploaded files<\/strong><\/p>\n<p>&#8216;uploaded-files&#8217; (if used) is an associative array of <strong>field_name =&gt; pathnames<\/strong> on the local drive to files that are uploaded. For example:<\/p>\n<p><code>uploaded_files =&gt; array ( 'field-name' =&gt; '\/path\/to\/the\/filename' )<\/code><\/p>\n<p><em>When there is a file upload, the information about it appears <span style=\"text-decoration: underline;\">twice<\/span><\/em><span style=\"text-decoration: underline;\">. <\/span><\/p>\n<ul>\n<li>First, it appears in the list of fields (where you see fname, lname, etc. above) as\u00a0<strong>&#8216;field-name&#8217; =&gt; &#8216;upload-filename&#8217;<\/strong><\/li>\n<li>Second it appears inside the\u00a0<strong>uploaded_files<\/strong> associative array\u00a0as <strong>&#8216;field-name&#8217; =&gt; &#8216;path-to-file&#8217; <\/strong>(use the same &#8216;field-name&#8217; in both).<\/li>\n<\/ul>\n<p>Files uploaded from a form post get saved to a temporary disk location. You can find this using the PHP <a title=\"_files\" href=\"http:\/\/php.net\/manual\/en\/reserved.variables.files.php\" target=\"_blank\">$_FILES<\/a> variable. Here is an example:<\/p>\n<pre><code>$data = (object) \u00a0array(\r\n    'title' =&gt; 'form-name',\r\n\u00a0\u00a0 \u00a0'posted_data' =&gt; array(\r\n    <span style=\"background-color: yellow;\">'upload1' =&gt; $_FILES['upload1']['name'],<\/span>\r\n    'email' =&gt; $_POST['email']),\r\n<\/code><span style=\"font-family: monospace;\">    'uploaded_files' =&gt; array (<span style=\"background-color: yellow;\">'fileupload' =&gt; $_FILES[\"upload1\"][\"tmp_name\"]<\/span> ));<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h3>Delete When Done<\/h3>\n<p>When done processing an uploaded file then <a title=\"unlink\" href=\"http:\/\/php.net\/manual\/en\/function.unlink.php\" target=\"_blank\">delete (unlink)<\/a> the files when done.<\/p>\n<p>&nbsp;<\/p>\n<h2>Calling the Plugin<\/h2>\n<p>&nbsp;<\/p>\n<h3>Directly use the Plugin<\/h3>\n<pre><code>require_once(ABSPATH . 'wp-content\/plugins\/contact-form-7-to-database-extension\/CF7DBPlugin.php');\r\n$plugin = new CF7DBPlugin();\r\n$plugin-&gt;saveFormData(&amp;$data);<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h3>Or Use a hook<\/h3>\n<p>Both CF7 and FSCF send form submission data to hooks that this plugin subscribes to. You can simply publish your form submission to one of those hooks. The advantage is that you do not need to include the CF7DBPlugin.php file, it is a de-coupled approach. The disadvantage is that any other plugin listening to the hook will also get the data (but maybe you want that&#8230;)<\/p>\n<p>You can use either hook. You don&#8217;t need to have these other plugin&#8217;s installed. A hook is just a name.<\/p>\n<p><strong>Use CF7&#8217;s Hook<\/strong><\/p>\n<p><code>do_action_ref_array( 'wpcf7_before_send_mail', array( &amp;$data) );<\/code><\/p>\n<p><strong>Or Use FSCF&#8217;s Hook<\/strong><\/p>\n<p>To Do: We need to have CFDBPlugin have its own hook to listen to.<\/p>\n<p><code>do_action_ref_array( 'fsctf_mail_sent', array( &amp;$data ) );<\/code><\/p>\n<p><strong>To Do: <\/strong><\/p>\n<p>On writing this I realize that the plugin should have its own hook so that people can use that decoupled approach without needing to worry about other hook subscribers. As of writing the current version is 1.8.3. I will put the following hook in version 1.8.4.<\/p>\n<p><code>do_action_ref_array( 'cfdb_submit', array( &amp;$data ) );<\/code><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Perhaps you wish to save data from a form of your own, not CF7 or FSCF. Or perhaps you just want to programatically push data into the DB. To do this there are two basic things you will need to know: How to struture your form data so that the plugin knows how to consume [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":102,"menu_order":4,"comment_status":"closed","ping_status":"closed","template":"page-without-sidebar.php","meta":{"jetpack_post_was_ever_published":false,"footnotes":""},"class_list":["post-377","page","type-page","status-publish","hentry"],"jetpack_shortlink":"https:\/\/wp.me\/P1mptf-65","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages\/377","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=377"}],"version-history":[{"count":10,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages\/377\/revisions"}],"predecessor-version":[{"id":382,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages\/377\/revisions\/382"}],"up":[{"embeddable":true,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages\/102"}],"wp:attachment":[{"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=377"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}