I have 2 input fields to input the duration of a CD song. The first input is for minutes and the second is for seconds
When submitting the form, I must insert in the db the duration either in seconds (minutes + seconds) or NULL
.
Here's my validation formula:
$warning = array();
function val_duration($min, $sec, $required = false) {
global $warning;
if (!empty($min) || !empty($sec)) {
if (!empty($min) && (!is_numeric($min) || !ctype_digit($min) || $min > 60)) {
$warning['min'] = 'The minutes must contain a positive numeric value between 1 and 60';
} else {
$min_in_sec = $min * 60;
}
if (!empty($sec) && (!is_numeric($sec) || !ctype_digit($sec) || $sec > 59)) {
$warning['sec'] = 'The seconds must contain a positive numeric value between 1 and 59';
} else {
$tmpsec = $sec;
}
$value = $min_in_sec + $tmpsec;
} else {
if ($required) {
$warning['duration'] = "The duration is a required field";
}
$value = NULL;
}
return $value;
}
// HTML
<input id="min" name="min" type="text" value="...">
<input id="sec" name="sec" type="text" value="...">
// validate
$min = ltrim(trim($_POST['min']), '0');
$sec = ltrim(trim($_POST['sec']), '0');
$duration_in_secs = val_duration($min, $sec, true); // duration
// mySQL
enter $duration_in_secs in db (either seconds or NULL)
$warning
doesn't seem to be accessible outside of the function? \$\endgroup\$150 seconds
(maybe they are getting that info in total seconds from a different source/app and don't want to make the conversion)? \$\endgroup\$