Exemple #1
0
    <div id="content" class="row">
        <div class="col-md-12">


            <?php 
$amount = $_POST["convertAmount"];
$unit = htmlentities($_POST["convertUnit"]);
$pattern = "/^[1-9][0-9]*\$/";
if ($unit != "noEntry") {
    $trimmedAmount = trim($amount);
    if (strlen($trimmedAmount) > 0) {
        preg_match($pattern, $amount, $matches);
        if (empty($matches)) {
            echo '<div class = "error">You did not input a valid number.</div>';
        } else {
            ConvertLiquid($amount, $unit);
        }
    } else {
        echo '<div class = "error">You did not enter a number.</div>';
    }
} else {
    $pattern = "/^[1-9][0-9]*\$/";
    $trimmedAmount = trim($amount);
    if (strlen($trimmedAmount) == 0) {
        echo '<div class = "error">You did not enter an amount.<br>You did not select a unit to convert.</div>';
    } else {
        if (strlen($trimmedAmount) > 0) {
            preg_match($pattern, $amount, $matches);
            if (empty($matches)) {
                echo '<div class = "error">You did not input a valid number.<br>You did not select a unit to convert.</div>';
            }
Exemple #2
0
    /* 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);
    }
}
?>

    <?php 
include 'partials/header.php';
?>

        <div class="container">
            <div class="page-header">
                <h1><i class="fa fa-tint fa-fw"></i> Welcome to the Liquid Conversion Tool</h1>