condition

How to conditionally write a row to the database when a field is empty.

Overview

The issue arises from incorrect PHP syntax and variable scope when checking if a form field is empty.
Use the correct object syntax to access the field value and ensure the variable is declared outside the conditional block to be properly returned.

Answered
ChronoForms v6
er ernst@volny.cz 29 Jul, 2019
how do I create a condition that if the field is empty, the row is written to the database?I can't write it using event switch or if condition
he healyhatman 29 Jul, 2019
{data:fieldname/default_value_if_field_missing} which is slightly different to if the field is there but empty
Could always use a switch: {data.empty:fieldname} and in true put your default value and false just {data:fieldname}
er ernst@volny.cz 29 Jul, 2019
I also invented a solution, but it doesn't work for me. can you advise me?
in logis PHP is
if($this->data["kurz"]  =='1'): $hop='1';
else : $hop='2';
endif;

return $hop;
and in event switcher: https://jmp.sh/A9bfJec, but I don't see the message
he healyhatman 29 Jul, 2019
I don't see where you set $hop or gave it a value in the first condition
he healyhatman 29 Jul, 2019
You sure did, which means it's not available outside of that else.
he healyhatman 29 Jul, 2019
Answer
1 Likes
Either use a switch with conditions 1 and * (in that order) or
if($this->data("kurz"))
return 1;
else
return 2;

Or for your code
$hop = ''; // Set this outside of the condition
if($this->data('kurz') == 1)
$hop = 1;
else
$hop = 2;
return $hop;
er ernst@volny.cz 30 Jul, 2019
Thanks a lot for your help, it works for me.
I wouldn't think of using parentheses instead of square brackets.
Also, because it was a deep night with us, I made other mistakes there.
Gr GreyHead 30 Jul, 2019
1 Likes
Hi Ernst,

The difference in the syntax is because $this here is an object, not an array. There are some similarities but a lot of differences too. There's an article here that tries to explain it.

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