A beginner question: I don't know how best to structure this bit of code, but basically it goes like this (pseudo code time):
if (form = submitted) {
submitted();
}
else {
printForm();
}
function submitted() {
process data from form;
if(errors = found) {
print warnings;
printForm();
} else {
submit to database;
}
}
function printForm() {
print form with databound elements;
}
I use the following bit of code to create a User object, but it seems weird to call it twice -- once in submitted() and once in printForm(), especially since submitted() calls printForm() if errors are found.
Unfortunately database access is required for processing the data from the form (checking for existing email address, etc), so I have to call the following bit of code in both submitted() and printForm()...
try {
$db = new Database();
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$user = new User($db);
}
catch (PDOException $e) {
echo "<p>Error connecting to database: </p>".$e->getMessage();
}
But my instincts tell me that this is bad. Is it? If so, how should I fix it?