Skip Navigation

[Resolved] CRED form: Automatically select value, if empty

This support ticket is created 3 years, 7 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 2 replies, has 2 voices.

Last updated by Lara 3 years, 7 months ago.

Assisted by: Christian Cox.

Author
Posts
#1729673

Tell us what you are trying to do?

I have a CRED form and I try to check, if a select field is not set / empty. If it's not set / empty, it should automatically assign a value during CRED form submission. I used the same code already for single text field and datepicker ... but here it doesn't work.

The code:

add_action('cred_before_save_data', 'func_set_select_field',10,1);
function func_set_select_field($form_data)
{
    // if a specific form
    $forms = array( 10441 , 12961 );
   
    if (in_array($form_data['id'], $forms)) 
    {
      
// if value of wpcf-rask-art-der-teilnehmerliste-termine field is empty
if (empty($_POST['wpcf-rask-art-der-teilnehmerliste-termine']['selector'])) {
// set wpcf-rask-bis-tag field as value 1
            $_POST['wpcf-rask-art-der-teilnehmerliste-termine'] = 1;  
        }
    }
}

I think the problem might be in this line:

if (empty($_POST['wpcf-rask-art-der-teilnehmerliste-termine']['selector'])) {

I am not really sure, what word I should use to describe the CRED select data type. I tried "select" and "selector" - but both doesn't work. I also searched for a documentation / list, where I can find all data types, so that I don't have to ask you guys everytime - but I didn't find anything. Sorry.

Is there any documentation that you are following?
https://toolset.com/forums/topic/re-open-cred-assign-value-of-another-field-if-field-is-empty/

Is there a similar example that we can see?
In the documentation

What is the link to your site?
hidden link

#1729967

Hello, the $_POST format for a datepicker field is more complex, but the $_POST format for a select field is simple. It only contains the selected option value. So you can change your conditional like this:

if (empty($_POST['wpcf-rask-art-der-teilnehmerliste-termine'])) {
// set wpcf-rask-art-der-teilnehmerliste-termine field as value 1  
        $_POST['wpcf-rask-art-der-teilnehmerliste-termine'] = 1;  
 }

Unfortunately there is no documentation available for all the different data types. Any time you are unsure, use print_r to output the entire $_POST superglobal format during a submission test. This will allow you to see the full data structure. You can write it to the log like this:

error_log(print_r($_POST, true));

Here is an example of the output logged from my Form:

 [wpcf-book-date-2] => Array
        (
            [display-only] => August 17, 2020
            [datepicker] => 1597622400
            [hour] => 8
            [minute] => 0
            [datetime] => 2020-08-17
            [timestamp] => 1597651200
        )

    [wpcf-book-embed] => Array
        (
            [0] =>
        )

    [wpcf-book-video] => Array
        (
            [0] =>
        )

    [book-ref-1] => 2458
    [wpcf-book-single-img-1] =>
    [wpcf-book-single-img-2] =>
    [wpcf-book-sel-1] =>
    [wpcf-book-rept-img-1] => Array
        (
            [0] =>
        )

    [wpcf-book-multi-to-wysi] => fafafa
    [wpcf-book-location] =>
    [toolset-extended-form-wpcf-book-location] => Array
        (
            [latitude] =>
            [longitude] =>
        )

Writing to the log is much faster than waiting for a supporter in the forum 🙂 You can see datepicker and address fields are more complex, each with an array of key/value pairs. Repeating fields contain an array of simple values.

Hope this helps, please let me know if you have follow-up questions.

#1730015

Many thanks Christian 🙂 and also many thanks for the explanation.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.