Skip Navigation

[Resolved] access form data in cred_save_data hook

This support ticket is created 5 years ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 2 replies, has 2 voices.

Last updated by charleneK 5 years ago.

Assisted by: Waqar.

Author
Posts
#1418755

Hello,
I"m using cred_save_data to intercept the submit of a form.

add_action('cred_save_data', 'this_save_data_action',10,2);
function this_save_data_action($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==33427)
    {
      Do somthing code. 
    }

}

I've got access to $_POST data from prevoius page. but how do I access the field data for the form itself. So say I have a field in the form called "myfield" I can access the $form_data['id'] but is it possible to get the $form_data['myfield']. ?

basically I'm using CURL to send some data to a webhook when the form is submitted.

here's my actual code:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
  
  // if a specific form
    if ($form_data['id']==33414)
    {
        if (isset($_POST['shared_room_matched']))
        {
           
         $curl = curl_init();
$url = '<em><u>hidden link</u></em>';
$myvar1 = $post_id;
$myvar2 = $form_data['myfield'];
$myvars = 'EventID=' . $myvar1 . '&data=' . $myvar2;

$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $myvars);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch, CURLOPT_HEADER, 0);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);

$response = curl_exec( $ch );
          
        }
    }

}

this works quite well to send the $post_id via myvar1 to the webhook. but I'm wondering how to access the fields data from the form itself.

thanks much++

#1421245

Hi Charlene,

Thank you for contacting us and I'd be happy to assist.

As explained in the documentation ( ref: https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data ), the "$form_data" includes information about the form itself, but not the form's field data.

To get the form's field data, you can use superglobal "$_POST" ( ref: hidden link )
, for example, "$_POST['myfield']" for the field with the field name "myfield".

Tip: For troubleshooting PHP values and submitted data, you'll find the "WP PHP Console" plugin very useful:
https://wordpress.org/plugins/wp-php-console/

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#1426737

My issue is resolved now. Thank you!