Skip Navigation

[Resolved] Access CRED form Select field value during cred_save_data

This thread is resolved. Here is a description of the problem and solution.

Problem:

When a user submits a CRED from, I need data within that form to either be deleted or saved depending on which option was chosen in a select field earlier in that same form.

The select field is called voyage-or-excursion and the field values saved to the database are simply 1 or 2.

If Option 1 is selected, I need the data in the field voyage-from-event to be deleted.
If option 2 is selected, I need the data to be saved.

I have tried a few variations of the below code but with no luck, it simply doesn't save anything to the field.
The add_post_meta function is tested and working, but does not work when the new IF functions are added for the select field. Can you advise on what may be the issue?

Solution:

How do you setup the select field "voyage-or-excursion", if it is a custom field created with Types plugin, you will need to add string "wpcf-" in the field slug, for example:
replace this line from:
if ( (!empty($_POST['voyage-or-excursion'])) && ( $_POST['voyage-or-excursion'] == '2' ) ) {

To:
if ( (!empty($_POST['wpcf-voyage-or-excursion'])) && ( $_POST['wpcf-voyage-or-excursion'] == '2' ) ) {

Relevant Documentation:

This support ticket is created 6 years, 9 months 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/Hong_Kong (GMT+08:00)

This topic contains 5 replies, has 2 voices.

Last updated by Luo Yang 6 years, 9 months ago.

Assisted by: Luo Yang.

Author
Posts
#619482

When a user submits a CRED from, I need data within that form to either be deleted or saved depending on which option was chosen in a select field earlier in that same form.

The select field is called voyage-or-excursion and the field values saved to the database are simply 1 or 2.

If Option 1 is selected, I need the data in the field voyage-from-event to be deleted.
If option 2 is selected, I need the data to be saved.

I have tried a few variations of the below code but with no luck, it simply doesn't save anything to the field.
The add_post_meta function is tested and working, but does not work when the new IF functions are added for the select field. Can you advise on what may be the issue?

// New Voyage Form - Save FROM Event Data
add_action('cred_save_data', 'save_voyage_from_event_data_action',10,2);
function save_voyage_from_event_data_action($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==90) {

	if ( (!empty($_POST['voyage-or-excursion'])) && ( $_POST['voyage-or-excursion'] == '2' ) ) {

	        if (isset($_POST['voyage-from-event'])) {

	            // add it to saved post meta
	            add_post_meta($post_id, 'wpcf-voyage-from-event', $_POST['voyage-from-event'], true);
		}
          }
     }
}
#619581

Hi,

How do you setup the select field "voyage-or-excursion", if it is a custom field created with Types plugin, you will need to add string "wpcf-" in the field slug, for example:
replace this line from:
if ( (!empty($_POST['voyage-or-excursion'])) && ( $_POST['voyage-or-excursion'] == '2' ) ) {

To:
if ( (!empty($_POST['wpcf-voyage-or-excursion'])) && ( $_POST['wpcf-voyage-or-excursion'] == '2' ) ) {

#619684

Hi Luo,

It looks o be working, I'll test further and mark as resolved when I'm sure.

For reference, below is the code for the New post for and the Edit post form...


// New Voyage Form - Save FROM Event Data
add_action('cred_save_data', 'save_voyage_from_event_data_action',10,2);
function save_voyage_from_event_data_action($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==90) {

	if ( (!empty($_POST['wpcf-voyage-or-excursion'])) && ( $_POST['wpcf-voyage-or-excursion'] == '2' ) && (isset($_POST['voyage-from-event']))) {


	        // add it to saved post meta
	        add_post_meta($post_id, 'wpcf-voyage-from-event', $_POST['voyage-from-event'], true);

          }

	  elseif ( (!empty($_POST['wpcf-voyage-or-excursion'])) && ( $_POST['wpcf-voyage-or-excursion'] == '1' ) && (isset($_POST['wpcf-voyage-from-event']))) {

		delete_post_meta($post_id,"wpcf-voyage-from-event");

	  }
     }
}

// Edit Voyage Form - Update FROM Event Data
add_action('cred_save_data', 'update_voyage_from_event_data_action',10,2);
function update_voyage_from_event_data_action($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==110)
    {
        if ( (!empty($_POST['wpcf-voyage-or-excursion'])) && ( $_POST['wpcf-voyage-or-excursion'] == '2' ) && (isset($_POST['voyage-from-event']))) {

       		update_post_meta($post_id, 'wpcf-voyage-from-event', $_POST['voyage-from-event']);

	}
	elseif ( ( $_POST['wpcf-voyage-or-excursion'] == '1' ) ) {

		delete_post_meta($post_id, 'wpcf-voyage-from-event' );

	  }
    }
}
#619850

Thanks for share the codes, it will help other users, and please update this thread if you still need assistance.

#620113

Everything seems to be working okay, thanks for the support!

#620312

You are welcome