send data api - post data not being passed

By: Mike Harding | Asked: 01/28/2023
ForumsCategory: Code Helpsend data api - post data not being passed
Mike Harding asked 1 year ago
I have implemented a form that has an action of send data api as a POST in form format. In the log I can see that the post data is being passed successfully in a value called frm_request as a long encoded string. All is good, that's what I expect. On the receiving php script, I cannot seem to grab the data from the $_POST array. In troubleshooting I've replaced the receiving php file with a simple print_r($_POST) and am writing that to a file, which has no content.  Here are snippets of the code, perhaps I'm doing something wrong. I just can't see it. First attempt, looping through the $_POST key/values foreach ($_POST as $key => $value) {
 $output = '<p>'.$key.'</p>';
 fwrite($fh, $output);

 foreach($value as $k => $v) {
 $outputk = '<p>'.$k.'</p>';
 fwrite($fh, $outputk);
 $outputv = '<p>'.$v.'</p>';
 fwrite($fh, $outputv);
 $outputh = '<hr />';
 fwrite($fh, $outputh);
 } } Second attempt with print_r() $post = print_r($_POST, true);
fwrite($fh, $post); Third attempt with var_dump $post = var_dump($_POST);
fwrite($fh, $post); I'm new to FF and PHP so I'm still learning how various things work, in no way an expert.
Mike Harding replied 1 year ago

So, I created a plain old html form with an action to point to my debug code and with the following modification, it works with both var_dump() and print_r().

ob_flush();
ob_start();
#$post = var_dump($_POST);
$post = print_r($_POST);
fwrite($fh, ob_get_flush());

When the same debug is called by send API data with POST, Form field format, etc. it results in an empty array () in the output file. Yet the log shows success 200 code for the post and has data like

frm_request
%5Bplanname%5D=testing+123&%5B10%5D=Fixed+Wired&%5B11%5D=123&%5B12%5D=No&%5B13%5

I remain stumped. Perhaps the send API data doesn't really send a post to the external script?

3 Answers
Victor Font answered 1 year ago
The PHP $_POST is an associative array. This means it's an array of mixed values including other arrays. A simple foreach loop will not return accurate values. Your code needs to determine what type of value $v is and execute a second or even third foreach for every nested array.
Mike Harding replied 1 year ago

Thank you for your response Victor. I've made some modifications and still have no result set. print_r() and var_dump() should expose whatever is in the array, correct? When I use a non-FF form, these dump the array contents. When I use the send data api from the FF form, the array appears to be empty. Do you have any ideas why that might be?

Victor Font replied 1 year ago

What Formidable hook are you using to execute your callback function?

Mike Harding replied 1 year ago

Sorry, I realized I didn't reply in the right spot, please see the below entry. POST is the action type on When entry is created.

Mike Harding answered 1 year ago
On the FF side, please see the attachment. On the receiving PHP side, no hooks, raw PHP as it is not a WP web server instance, it\'s remote instance and with another job to do when the form is submitted by FF on a remote server. The receiving code looks like this at the moment: <?php $filename = \"/label/post.log\"; $fh = fopen($filename, \"a\"); ob_flush(); ob_start(); $post = var_dump($_POST); fwrite($fh, ob_get_flush()); fclose($fh); ?> Mind you, all I\'m trying to do right now is prove that I can receive $_POST parameters passed from FF. When I test using the code above using a simple HTML non-FF form, it prints the contents of $_POST faithfully with contents i.e.: Array (     [firstname] => Mike     [lastname] => Harding     [form_submitted] => 1 ) When I post using the FF form, it has these contents: Array ( )  
Attachments
Mike Harding replied 1 year ago

Sorry, the form condensed the php code into an ugly mass.

Victor Font replied 1 year ago

I don't think it's ever going to work the way you want it to. In an API, POST is not a variable. It's an API's method property that can be POST, GET, PUT, PATCH, DELETE. The method determines how the data is handled on the receiving end.

The PHP $_POST associative array is a PHP session variable. If the connection to the user session is severed, $_POST no longer exists. You are passing Formidable form values to your raw code through the API add-on. The add-on is designed to work with APIs. API REQUESTs are formatted as JSON. You can see what an API request looks like in the network tab of your browser's development tools. Look at the XHR object whenever you see admin-ajax.php executed on your WordPress site. For transfers with the 200 response, the XHR object has both the REQUEST and RESPONSE. The JSON you send from WordPress is in the REQUEST BODY and needs to be converted to an object or array in your PHP code before you can scan it.

I have absolutely no idea how to tell you how to read and process a JSON REQUEST in your raw code. I don't know how your raw code can access it. Good luck in figuring this out. Come back and let us know how you solve it.

Mike Harding replied 1 year ago

Well, not the answer I was hoping for! But I appreciate it nevertheless. Thanks for your ideas Victor and I will come back to the forum and post how it was solved when that happens.

Mike Harding answered 1 year ago
After banging my head against this issue for a few days, I asked the FF team for some help. In the process of troubleshooting the answer became apparent. In FF in the send data api add on, I had defined the fields to be sent in the data mapper portion. Doing that moves the response from $_POST to a custom API output. I could see this in the web server error log once dumpio was enabled. Deleting the action, then re-adding it without the data mapping, allows data to be accessed as one would expect in a garden variety php script by interrogating $_POST Appreciate the help and engagement from Victor on this. Hopefully this answer will help someone going forward.

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right