I have a form with 3 input boxes, 1 radio button, 3 dropdown lists and of course a submit button.
1 dropdown box must be an integer input & 1 must be alphabetic only.
I have currently got very basic validation functionality set up which can:
1) Check that all the require fields have been supplied with some data. 2) Check that one input box is ints only. 3) check that another is letters only.
If any input is left out, I can output an error. What I'm trying to do is determine which fields were left out and give the user a more specific feedback to the right of the relevant field
This is the code so far (it's probably not the most efficient solution):
if (isset($_POST['submit'])){
$submit = ($_POST['submit']);
$title = $_POST['title'];
$duration = $_POST['duration'];
$director = $_POST['director'];
/*if (isset($_POST['cert'])){
echo "cert set: ".$_POST['cert'];
}*/
//If any of the (title, duration, director) are empty or if any of the (day, month, year) are unchanged or if the cert isn't set i.e radio button checked
//Error
if (empty($title) || empty($duration) || empty($director) || !isset($_POST['cert']) || ($_POST['day'] == "DD") || ($_POST['month'] == "MM") || ($_POST['year'] == "YYYY")){
echo "Error";
} else {
//Check if duration entered is an integer
if (is_int($duration) && ctype_alpha($director)) {
$cert = $_POST['cert'];
$day = $_POST['day'];
$month = $_POST['month'];
$year = $_POST['year'];
$day = convertToTwoDigit($day);
$month = convertToTwoDigit($month);
$year = convertToTwoDigit($year);
$date = $year."/".$month."/".$day;
echo "<br>Title: ".$title."</br>"."Duration: ".$duration."</br>"."Director: ".$director."</br>"."Cert: ".$cert."</br>Relesed on: ".$date;
//Add values into database:
if ($insert = $db->query("
INSERT INTO titles (cert, filmtitle, releaseDate, filmDuration, director) VALUES ('$cert', '$title', '$date', '$duration', '$director')
")){
echo "You have added a film";
} else {
echo "Error adding";
}
} else {
echo "director or duration error";
}
}
}
What is required to improve the validation to be more specific. Would some sort of array work ?