Page 1 of 1
PHP: Optimal Form Re-Use/Structure
Posted: Tue Aug 30, 2005 10:18 am
by cfytable
I have a form that my users will use to create a record in the database. Some of these same users later will need to modify/update this record. In both cases, the HTML form would be exactly the same, though, in the case of updating, it would be pre-populated with existing data from the database.
Rather than maintain several different instances of the same HTML form in different PHP pages, I'm looking for the simplest reusable setup. I need to satisfy the following requirements.
1) Blank form / Pre-Populated form, based on whether a Create or Modify instance
2) Insert/Update logic, based on whether a Create or Modify instance
3) Ability to pre-populate either with data from database in Modify instance, and, in the case of Create or Modify, re-display of user-submitted data if it fails to satisfy validation criteria. In other words, if the user modified the existing data and tried to submit, but it did not pass a validation test, they would not have to retype all their changes.
Any ideas on how best to structure in terms of # of pages, functions, etc.? Any help would be greatly appreciated.
Posted: Tue Aug 30, 2005 10:42 am
by s.dot
Basically you'll need to check if a record exists in the database (if they're modifying/updating) then populate the form based on that result.
if there is a record in the database, populating your forms would look like this.
Code: Select all
<input type="text" name="name" value="<? if($row['field']){ echo $row['field']; } ?>">
Of course if there is no record, then this value will be blank, and you'll have a blank form if all of the values are blank.
As far as validating a modified record goes, using your existing validation for when the record is first created, should be suitable.
Posted: Tue Aug 30, 2005 11:10 am
by cfytable
Thanks for the quick reply.
So, should I have a query string to distinguish whether it's an UPDATE or CREATE scenario, which would then allow me to serve up the appropriate logic? With regard to the VALUE fields in the forms, what about the case where the user tries to submit a modified version of pre-populated data--in that case, what logic would I need to tell the form not to look at the database data, but rather the modified data that the user tried to submit, but failed to pass the validation test?
As if that weren't enough, my form has checkboxes and radio buttons--to get the form to remember what the user tried to input, how would that affect the VALUE fields?
Any help would be GREATLY appreciated. I'm very confused. ;)
Posted: Tue Aug 30, 2005 11:41 am
by nielsene
On method I've used in the past with mixed success.
Create a $formVars array (or class, your choice):
The keys of the $formVars are the names of the fields. The values are the value to display.
You can initialize the whole structure to either the values from the DB or all blanks/defaults (for UPDATE versus INSERT).
Your form validation script can place the new values into the $formVars (after suitable cleansing) for redisplay.
I've used this both on self-submitting forms and on redirecting forward/backward forms, with minor difference and have been happy with it. (Its not perfect, but it works well for small-medium sized applications. A more OOP version is preferred.
In abbreviated PHP (self-submitting version);
Code: Select all
$formVars["Title"]="";
$formVars["FirstName"=""; // ...
$validated=TRUE;
if (isset($_POST["Submit"]) {
if ($_POST["Title"]=="") {
$formVars["Title"]=""; $formVars["Title_msg"]="You must enter a title";
$validated=FALSE;
} else {
$formVars["Title"]=htmlspecialchars(trim($_POST["Title"]));
}
// .. preform more validations.
if ($validated) {
// insert to DB, carry out action, whatever, often forward to a success page, or
// clear out $formVars to allow an immedate new entry, etc
}
} else if (isset($_POST["LookUp"])) {
$id = $_POST["LookUp"];
if (preg_match($somePattern,$id)) {
// load from database into FormVars
}
}
// now display the form, always using the value from $formVars for each field,
// possibly coupled with a if (isset($fieldname."_msg")) for displaying error messages.