Skip Navigation

[Resolved] CRED form: change select value, depending of the status of another select field

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

Problem:

I have a CRED form with three select fields. If select field A has a certain value, select field C should change it's value through custom code. The custom code works fine. The only problem, that I am facing is, that I don't know, how I should apply the if condition with the CRED select field.

Solution:

It is a custom PHP codes problem, see the solution here:

https://toolset.com/forums/topic/cred-form-change-select-value-depending-of-the-status-of-another-select-field/#post-1726535

Relevant Documentation:

https://www.php.net/manual/en/language.operators.comparison.php

This support ticket is created 3 years, 8 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 3 replies, has 2 voices.

Last updated by Lara 3 years, 8 months ago.

Assisted by: Luo Yang.

Author
Posts
#1726275

Tell us what you are trying to do?
I have a CRED form with three select fields. If select field A has a certain value, select field C should change it's value through custom code. The custom code works fine. The only problem, that I am facing is, that I don't know, how I should apply the if condition with the CRED select field. The code:

add_action('cred_before_save_data', 'update_zeiteinteilung',10,1);
function update_zeiteinteilung($form_data)
{
    // if a specific form
    $forms = array( 12807 );
   
    if (in_array($form_data['id'], $forms)) {

      
if ($_POST['wpcf-rask-von-uhrzeit-15-minuten'] = 2) {
      
$von_zeit_30 =  $_POST['wpcf-rask-von-uhrzeit'];
$bis_zeit_30 =  $_POST['wpcf-rask-bis-uhrzeit'];
  
$new_von_zeit_15 = $von_zeit_30  * 2 - 1;     
$new_bis_zeit_15 = $bis_zeit_30  * 2;
    
  
$_POST['wpcf-rask-von-uhrzeit-15-minuten'] = $new_von_zeit_15; 
$_POST['wpcf-rask-bis-uhrzeit-15-minuten'] = $new_bis_zeit_15;
  
       }
   } 
}

The code line with the problem ..

if ($_POST['wpcf-rask-von-uhrzeit-15-minuten'] = 2) {

The custom field content of the "rask-von-uhrzeir-15-minuten" field can be 1, 2 or 3. Default is 1.
The custom code should only be executed, if the value is 2

Is there any documentation that you are following?
no

Is there a similar example that we can see?
no

What is the link to your site?
hidden link

#1726535

Hello,

There is a problem in your custom PHP codes, this line:

if ($_POST['wpcf-rask-von-uhrzeit-15-minuten'] = 2) {

Will always set var $_POST['wpcf-rask-von-uhrzeit-15-minuten'] value to 2, it does not check the value at all.

In your case, you can change that line as below:

if ($_POST['wpcf-rask-von-uhrzeit-15-minuten'] == 2) {

More help:
hidden link
$a == $b Equal TRUE if $a is equal to $b after type juggling.

#1726707
2020-08-04_Toolset-Select-Field.PNG

Many thanks for your explanation, Luo. I wasn't aware, that ..

if ($_POST['wpcf-rask-von-uhrzeit-15-minuten'] = 2) {  

... sets the value to 2. But it does make sense.

I already tried this ...

if ($_POST['wpcf-rask-von-uhrzeit-15-minuten'] == 2) {

... last night. It doesn't work. The code after that line, will not be executed. Do I need to include the data typ als well? Like in this example: $_POST['wpcf-rask-bis-tag']['datepicker'] ? I am a little bit lost ..

Kind regards
Lara

#1726753

My issue is resolved now. Thank you!

After I uploaded the image, I realized, that I accidently used the wrong field. I am truely sorry.
With ...

if ($_POST['wpcf-rask-art-der-teilnehmerliste-tag']['radio'] == "2") {

... it works perfectly.

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