<h2><strong>Thank you for using our deposit calculator</strong></h2> <p>Unfortunately we can not calculate the payments because you entered the following invalid data:</p> <ul> <?php // Iterate through each post variable and display relevant errors foreach ($labels as $label) { // Set default checkbox $_POST[$labels['contactTime']] = isset($_POST[$labels['contactTime']]) ? $_POST[$labels['contactTime']] : []; // Check for required fields if (!is_valid($_POST[$label]) && $label !== 'ContactTime') { echo "<li>" . $label . ' ' . $rules['empty'] . "</li>"; } // Check that principal amount is numeric and greater than zero if ($label == $labels['principal']) { if (is_valid($_POST[$label]) && is_num_above_zero($_POST[$label]) == false) { echo "<li>" . $labels['principal'] . $rules['numeric_above_zero'] . "</li>"; } } // Check that interest rate is numeric and not negative if ($label == $labels['interest']) { if (is_valid($_POST[$label]) && is_num_not_negative($_POST[$label]) == false) { echo "<li>" . $labels['interest'] . $rules['numeric_not_negative'] . "</li>"; } } // Check for valid phone number if ($label == $labels['phone']) { if (is_valid($_POST[$label]) && is_valid_phone($_POST[$label]) == false) { echo "<li>" . $labels['phone'] . $rules['valid_phone'] . "</li>"; } }
<?php /* Define global field array for use in custom error messages */ $requiredField = array('amount' => 'amount', 'unit' => 'unit'); /* Define validation rule messages */ $errorMessage = array('no_amount' => 'You did not enter an amount', 'no_unit' => 'You did not enter a unit to convert to', 'num_above_zero' => 'The entered amount must be numeric and greater than zero'); /* Define error array */ $errors = array(); /* If the convert button was pressed */ if (isset($_POST['convert'])) { /* Check if an amount has been entered */ if (!is_valid(sanitize_input($_POST[$requiredField['amount']]))) { array_push($errors, [$requiredField['amount'] => $errorMessage['no_amount']]); } /* If an amount has been entered and is valid, check if numeric and greater than zero */ if (is_valid(sanitize_input($_POST[$requiredField['amount']])) && !is_num_above_zero($_POST[$requiredField['amount']])) { array_push($errors, [$requiredField['amount'] => $errorMessage['num_above_zero']]); } /* Check if a unit has been selected */ if ($_POST[$requiredField['unit']] == 'default') { array_push($errors, [$requiredField['unit'] => $errorMessage['no_unit']]); } /* If there are no errors, convert! */ if (!has_items($errors)) { /* Send the submitted values to the convert function */ $convertedValue = ConvertLiquid($_POST[$requiredField['amount']], $_POST[$requiredField['unit']]); } } else { if (isset($_POST['reset'])) { unset($errors); }