php if empty

How to conditionally calculate a form field only when it's empty.

Overview

The issue was an incorrect conditional check and syntax errors in the PHP code.
Use the empty() function to check if the price field is empty, then calculate the value using other form fields only in that case.

Answered
ma marcinwolejko 20 Mar, 2015
Hi guys.
It's been a while since I asked for help.
I'm struggling with a short piece of code for my form.
I have a field with price which can be either calculated by a php script of filled in by hand by a user.
Now I want to write a condition that would check whether a user manually filled in the price and then leave it as it is or if the form field 'price' is empty run a php script.

I already have managed to write this piece of code but to my despair it does not work.
Can you help me out debuggin it?

<?php
if (!empty($form->data['price'])) {
$form->data['price'] = $form->data['onevalue'] * $form->data['secondvalue'] + 10;
}
else $from->dara['price];
?>


Thanks in advance!

Marcin
Gr GreyHead 20 Mar, 2015
Answer
1 Likes
Hi Marcin,

Please try
<?php
if ( empty($form->data['price']) ) {
  $form->data['price'] = ( $form->data['onevalue'] * $form->data['secondvalue'] ) + 10;
}
?>

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