If else condition help needed
-
Hi, i have a number field in which user will enter any value. Now, I want to use if else condition based on that value. how I will use it?
-
Hello @asjad492
Thank you very much for using our plugin. You have different alternatives based on your equation’s requirements.
The plugin includes the
IF
operation. It has the structureIF(condition, result if the condition is true, result if the condition is false)
. Or, you can use theif
conditional statementif(condition){operations if the condition is true} else {operations if the condition is false}
Javascript is a case-sensitive language, please, do not confuse the
IF
conditional operation with theif
conditional statement.For example, assuming the number field is the fieldname1, and you want to duplicate its value if it is less than 10, or increase it by 5 otherwise.
Insert a calculated field in the form and enter the equation:
IF(fieldname1<10, fieldname1*2, fieldname1+5)
The same equation can be implemented by using
function
structure and theif
conditional statement:(function(){ if(fieldname1 < 10){return fieldname1*2;} else {return fieldname1+5;} })()
Best regards.
Thanks for your response. Can you please check if following code is correct?
(function(){ if(AND(fieldname16 == 'Normales Angebot (Jetzt kaufen)', fieldname31<1.990, fieldname14 == 'Weniger als 320', fieldname17 == 'Keine')){ return SUM(fieldname31*0.11, fieldname30*0.5); } else if (AND(fieldname16 == 'Normales Angebot (Jetzt kaufen)', fieldname31>1.990, fieldname14 == 'Weniger als 320', fieldname17 == 'Keine')) { return SUM(fieldname31*0.02, fieldname30*0.5); })();
Hello @asjad492
You forgotten a close parenthesis.
(function () { if (AND(fieldname16 == 'Normales Angebot (Jetzt kaufen)', fieldname31 < 1.990, fieldname14 == 'Weniger als 320', fieldname17 == 'Keine')) { return SUM(fieldname31 * 0.11, fieldname30 * 0.5); } else if (AND(fieldname16 == 'Normales Angebot (Jetzt kaufen)', fieldname31 > 1.990, fieldname14 == 'Weniger als 320', fieldname17 == 'Keine')) { return SUM(fieldname31 * 0.02, fieldname30 * 0.5); } })();
Best regards.
yes, I added. it calculates the same value regardless of the > or < condition in the above code. What to do? it always gives value based on the > condition instead of <
Hello @asjad492
I’m sorry, but I cannot identify the satisfied conditions if I don’t know the fields’ values.
Please, indicate the URL to the page that includes the form and the fields’ values.
Best regards.
I am saying it calculates the value. But for > < condition, it gives the same value. For example, if fieldname31<1.990, the result should show fieldname31*0.11. But it still shows fieldname31*0.02, which is condition for fieldname31>1.990
Hello @asjad492
The equation always enters into one of the conditional statements, and that happens only if the fields’ values satisfy that condition.
Best regards.
Is the following code correct? it is giving error.
if (AND(fieldname16 == 'Normales Angebot (Jetzt kaufen)', fieldname31<1990, fieldname17 == 'Keine', fieldname26 == 'Eurozone und Schweden')) { if (fieldname31<10) { return SUM(fieldname31*0.11, 0.05, fieldname31*0, fieldname30*0.5,0); } else { return SUM(fieldname31*0.11, 0.35, fieldname31*0, fieldname30*0.5,0); } }
Hello @asjad492
When you use the
return
instruction orif
conditional statement, you must implement the equation by using thefunction
structure.(function(){ if (AND(fieldname16 == 'Normales Angebot (Jetzt kaufen)', fieldname31 < 1990, fieldname17 == 'Keine', fieldname26 == 'Eurozone und Schweden')) { if (fieldname31 < 10) { return SUM(fieldname31 * 0.11, 0.05, fieldname31 * 0, fieldname30 * 0.5, 0); } else { return SUM(fieldname31 * 0.11, 0.35, fieldname31 * 0, fieldname30 * 0.5, 0); } } })
Note I can check the equation’s structure only because I don’t know your fields values.
Best regards.
So, previous code worked. Now I wrote another script. It isn’t working.
(function(){ if (AND(fieldname16 == 'Auktionsangebot', fieldname20 == 'Immobilien (# 10542)', fieldname21 == '10', fieldname34<1990, fieldname17 == 'Keine', fieldname26 == 'Eurozone und Schweden')) { if (fieldname34<10) { return SUM(1, 0.05, fieldname34*0, fieldname30*0.5,0); } else { return SUM(1, 0.35, fieldname34*0, fieldname30*0.5,0); } } else if (AND(fieldname16 == 'Auktionsangebot', fieldname20 == 'Immobilien (# 10542)', fieldname21 == '10', fieldname34<1990, fieldname17 == 'Bis zu 24 Fotos hinzufügen', fieldname26 == 'Eurozone und Schweden')) { if (fieldname34<10) { return SUM(1, 0.05, fieldname34*0, fieldname30*0.5,0); } else { return SUM(1, 0.35, fieldname34*0, fieldname30*0.5,0); } } })();
Hello @asjad492
The equation structure is not wrong. So, if the equation is not working, the issue is caused by the fields’ values. By the way, there are some points to improve in your equation. For example, in the piece of code:
return SUM(1, 0.05, fieldname34 * 0, fieldname30 * 0.5, 0);
Summing zero or
fieldname34 * 0
has no sense because they do not affect the result.Furthermore, when you repeat the same conditions in every conditional statement give the idea you need a new conditional statement level:
(function () { if (AND(fieldname16 == 'Auktionsangebot', fieldname20 == 'Immobilien (# 10542)', fieldname21 == 10, fieldname34 < 1990, fieldname26 == 'Eurozone und Schweden')) { if (fieldname17 == 'Keine') { if (fieldname34 < 10) { return SUM(1, 0.05, fieldname30 * 0.5); } else { return SUM(1, 0.35, fieldname30 * 0.5); } } else if (fieldname17 == 'Bis zu 24 Fotos hinzufügen') { if (fieldname34 < 10) { return SUM(1, 0.05, fieldname30 * 0.5); } else { return SUM(1, 0.35, fieldname30 * 0.5); } } } })();
By the way, in the equation, you are repeating the operations. It does not require different conditional statements at all.
Best regards.Thanks. Can you also check this one? It doesn’t.
(function(){ if (AND(fieldname16 == 'Normales Angebot (Jetzt kaufen)', fieldname18 == 'Ja', fieldname38 == 'Auto und Motorrad: Fahrzeuge ( # 9800 )', fieldname19 == 'Keine')) { return SUM(MIN(SUM(19,fieldname31*0.05),99)),0); } else if (AND(fieldname16 == 'Normales Angebot (Jetzt kaufen)', fieldname18 == 'Ja', fieldname38 == 'Auto und Motorrad: Fahrzeuge ( # 9800 )', fieldname19 == 'Bis zu 12 Fotos hinzufügen')) { return SUM(MIN(SUM(19,fieldname31*0.05),99),0); } else if (AND(fieldname16 == 'Normales Angebot (Jetzt kaufen)', fieldname31<1990, fieldname17 == 'Bis zu 24 Fotos hinzufügen', fieldname26 == 'Eurozone und Schweden')) { if (fieldname31<10) { return SUM(fieldname31*0.11, 0.05, fieldname31*0, fieldname30*0.5,0); } else { return SUM(fieldname31*0.11, 0.35 fieldname31*0, fieldname30*0.5,0); } } })();
But, when I use one of the block below. it works.
(function(){ if (AND(fieldname16 == 'Normales Angebot (Jetzt kaufen)', fieldname18 == 'Ja', fieldname38 == 'Auto und Motorrad: Fahrzeuge ( # 9800 )', fieldname19 == 'Bis zu 12 Fotos hinzufügen')) { return SUM(MIN(SUM(19,fieldname31*0.05),99),0); } else if (AND(fieldname16 == 'Normales Angebot (Jetzt kaufen)', fieldname31<1990, fieldname17 == 'Bis zu 24 Fotos hinzufügen', fieldname26 == 'Eurozone und Schweden')) { if (fieldname31<10) { return SUM(fieldname31*0.11, 0.05, fieldname31*0, fieldname30*0.5,0); } else { return SUM(fieldname31*0.11, 0.35 fieldname31*0, fieldname30*0.5,0); } } })();
Hello @asjad492
The number of the open and close parentheses in your first equation are incorrect. Each open parenthesis requires a close parenthesis.
I’m sorry, but we cannot implement your project via the WordPress forum.
If you need a custom coding service to implement your project, you can contact us directly through the plugin website: Custom Coding
Best regards.
Hi, I double-checked. Parentheses are correct.
Hello @asjad492
No, they are incorrect. For example, in this piece of code:
return SUM(MIN(SUM(19,fieldname31*0.05),99)),0);
There are three open parentheses but four for close.
Best regards.
- The topic ‘If else condition help needed’ is closed to new replies.