Forums

Load correct data into field A depending on if value of field B is 1 or 2

pat01 11 Oct, 2019
Hello

I have following situation:
Using a form “addresses” users can store two addresses: private and business.
A dropdown menu allows them to define what address will be the main address.

In database table chrono_chronoforms_data_form-demo I have columns
user_id
phone_ private
phone_business
city_private
city_business
main_address

Field main_address contains either value 1 or 2.
Value 1 = main address is phone_private and city_private
Value 2 = main address is phone_business and city_business


On front page I have another form called “subscription”. It contains two textfields:
phone
city
Now the user logs in and opens this form.

Depending on if the database field “main_address” contains 1 or 2, the form should automatically load the correct phone number and city.


My approach is as follows:

Add a read_data action and receive value of field main_address.
In the “found” section I add action “Events switcher”.
I add events 1 and 2.
In event 1 I add a read_data to get value of field phone_private and city_private.
In event 2 I add a read_data to get value of field phone_business and city_business.

The debugger gives me this:
Array
(
    [read_data1] => Array
        (
            [log] => Array
                (
                    [0] => SELECT `Address1`.`main_address` AS `Address1.main_address` FROM `chrono_chronoforms_data_form-demo` AS `Address1` WHERE `Address1`.`user_id` = '610' LIMIT 100;
                )

            [var] => Array
                (
                    [Address1] => Array
                        (
                            [main_address] => 1
                        )

                )

        )

)




Now the problem I have: Even if value 1 is read from main_address, CF does not execute the action in “event 1”. No matter if I add a read_data, a custom code or a debugger. Nothing happens.

What goes wrong here?

Thanks.
Patrick
pat01 12 Oct, 2019
I got one step further:

In the Events switcher and there in the data provider field I replaced {data:main_address} with
{var:read_data1.Address1.main_address}

Now I get the data from data_read action in event 1.

But I don't know how to load the pone number and city into the form fields.
In the view section it can be done by entering following code into field "value":

{var:address1.Address_Private.phone_private}

This works!

But I need this dynamically, because for the phone field it either can be
{var:address1.Address_Private.phone_private}
or
{var:address2.Address_Business.phone_business}
depending on if main_address is 1 or 2.

The same goes for the second textfield "city".

How can I get correct data?

Thanks.
Patrick
GreyHead 12 Oct, 2019
Hi Patrick,

Personally I'd probably use one query that gets both addresses, then use PHP to add the correct one to the form data.

Bob
pat01 12 Oct, 2019
Hi Bob and thanks

OK, so my approch might not be the correct one...🙂

Inside the events switcher I have given both read_data same name and same model name. This allows me to use this:
{var:user_address.Address.phone_private}
{var:user_address.Address.city_private}

Now only phone_private and city _private should be replaced by a dynamic value. I did try
{var:user_address.Address[1]}

but this is not working.

It works if I add this to the form field:


But if this approach is not the correct way to do this, I'll switch to a PHP solution.
Any suggestions?


Patrick
GreyHead 12 Oct, 2019
Hi Patrick,

If you have it working then stay with that. I don't think that there is one *right* way to do this.

Bob
pat01 12 Oct, 2019
Hi Bob

Yes, many ways lead to Rome...

Nevertheless I did try using PHP:

I use a read_data to get all data:
Array
(
[read_data1] => Array
(
[log] => Array
(
[0] => SELECT `Address1`.`aid` AS `Address1.aid`, `Address1`.`user_id` AS `Address1.user_id`, `Address1`.`created` AS `Address1.created`, `Address1`.`modified` AS `Address1.modified`, `Address1`.`phone_private` AS `Address1.phone_private`, `Address1`.`phone_business` AS `Address1.phone_business`, `Address1`.`city_private` AS `Address1.city_private`, `Address1`.`city_business` AS `Address1.city_business`, `Address1`.`main_address` AS `Address1.main_address` FROM `chrono_chronoforms_data_form-demo` AS `Address1` WHERE `Address1`.`user_id` = '610' LIMIT 100;
)

[var] => Array
(
[Address1] => Array
(
[aid] => 1
[user_id] => 610
[created] => 2019-10-12 04:47:46
[modified] => 2019-10-12 05:44:19
[phone_private] => 111 111 11 11
[phone_business] => 222 222 22 22
[city_private] => Berlin
[city_business] => London
[main_address] => 2
)

)

)

)

Then I use this PHP code:

$main_address = $this->data("main_address", "");

if ($main_address == 1) {
$phone = $this->data("phone_private", "");
}
if ($main_address == 2) {
$phone = $this->data("phone_business", "");
}

echo $phone;
echo "Test";
?>

But I can't get value of
$this->data("main_address", "")
The "Test" is echoed, so PHP is executed.

Why is $main_address empty and how can I get $phone into the value field in the view section?

Patrick
pat01 12 Oct, 2019
It works using this code:
<?php

$main_address = $this->get("read_data1.Address1.main_address", "");
$phoneP = $this->get("read_data1.Address1.phone_private", "");
$phoneB = $this->get("read_data1.Address1.phone_business", "");
$cityP = $this->get("read_data1.Address1.city_private", "");
$cityB = $this->get("read_data1.Address1.city_business", "");

if ($main_address == 1) {
$phone = $phoneP;
$city = $cityP;
}

if ($main_address == 2) {
$phone = $phoneB;
$city = $cityB;
}

echo $phone;
echo $city;

?>

Last step: How can I load $phone and $city as value into the form fields?

<php echo $phone; ?>

is not working.

Thank you.

Patrick
GreyHead 13 Oct, 2019
1 Likes
Hi Patrick,

Use
$this->data("field_name", "value", true);
e.g.
$this->data("phone", $phone, true);
Please see this FAQ for some more info.

Bob
pat01 13 Oct, 2019
Hi Bob

Thanks.
The difficulty I have is, I have two returns:
return $this->data("phone", $phone, true);
return $this->data("city", $city, true);

And in the views tab I have two textfields: one for phone and one for city.

{var:php3} loads the first return which is "phone". I can't get "city"

Following all are not working:
{var:php3.city}
{var:php3[city]}
{var:php3(city)}





Adding

$this->data("phone", $phone, true);

into the "Value" field is not working either, it outputs the whole string.

I did check the FAQ but I did not find the correct code.

What do I have to add into the Value field?

Thanks.
Patrick
GreyHead 13 Oct, 2019
2 Likes
Hi Patrick,

I don't think you need the 'return's in your code - you are setting values in the form data here - not returning them.

Bob
pat01 14 Oct, 2019
Answer
Thanks a lot Bob, for the hint.

It works now. Here's the working code in case someone else is looking for such a solution:
<?php

$main_address = $this->get("read_data1.Address1.main_address", "");
$phoneP = $this->get("read_data1.Address1.phone_private", "");
$phoneB = $this->get("read_data1.Address1.phone_business", "");
$cityP = $this->get("read_data1.Address1.city_private", "");
$cityB = $this->get("read_data1.Address1.city_business", "");

if ($main_address == 1) {
$phone = $phoneP;
$this->data("phone", $phone, true);
$city = $cityP;
$this->data("city", $city, true);
}

if ($main_address == 2) {
$phone = $phoneB;
$this->data("phone", $phone, true);
$city = $cityB;
$this->data("city", $city, true);
}

?>

And the content of the textfields "Value" fields:
{var:php3.phone}
{var:php3.city}

Patrick
This topic is locked and no more replies can be posted.