|
|
|
Problem: Certain form fields should be required, but we don't want to
hard-code these in as an array. We want to pull the required field names
from a database so our code is more extensible. Then, we want to check each
form field submitted, see if it is required, and if so, generate an error
message if that required field is blank.
Solution: There are several parts to the solution. First of all, create a
database table with all of the form field names, and whether or not they
are required. (I went further and created type of form field, default
value, etc. so I could generate the entire form from a database.) Here's a
basic table with what you need:
field_name required
fname yes
lname yes
wphone no
Next, create your form with ALL form field names being an array. For this
example, I use the array add[]. A sample form field pulled from a database
would look like this:
<input type=\"text\" name=\"add[$field_name]\"
size=\"$size\" value=\"$default_value\">
Here is the code that parses the form and checks for required fields:
# check to make sure required fields are filled in
$query = "select field_name from form_fields where
required=\"yes\"";
$result = mysql_query ($query) or die ("The server is down. Here's
what
MySQL said: " . mysql_error());
$required_fields = array();
while ($row = mysql_fetch_array ($result)) {
$field_name = $row['field_name'];
array_push ($required_fields, $field_name);
};
while(list($key,$val) = each($required_fields)) {
if (!strlen($add[$val])) {
$errors[$key] = "$val is a required field.";
# above line will store the form field name,
e.g. "fname is a required
field."
};
}; # END WHILE
if(is_array($errors)) {
while(list($key,$val) = each($errors)) {
print "Error! $val <br>";
# for every field that is blank, above line
will print "Error:
$field_name is a required field."
# this part can be easily customized.
};
}
else {
# here's where you put the "success" message, add
entries to database, etc.
};
I hope this helps those of you who want to make your forms extensible. I
owe most of this to the PHP mailing list! You guys ROCK!
Erica
|
| phpFormGenerator for Dynamic Form Generation from MySQL Categories : PHP, PHP Classes, MySQL, Databases, HTML and PHP | | | Functions for loading images into a MySQL database and displaying them. Categories : Graphics, HTML and PHP, MySQL, PHP, Databases | | | Dynamically generated pop-ups (Select items) Categories : PHP, HTML and PHP, MySQL, Databases | |
| | mySQL/PHP/search with multientry
form and table output with colored rows Categories : PHP, Beginner Guides, MySQL, HTML and PHP, Databases | | | Creating thumbnails from MySQL Blobs online Categories : PHP, MySQL, Graphics, HTML and PHP, Databases | | | Function to do live population of HTML's <Select> tag from a Table Categories : PHP, MySQL, HTML and PHP, Databases | | | Storing / Retrieving pictures from database This can be used for banner / ads exchange. very useful for people developing big portal with ads in the site........ Categories : PHP, MySQL, Databases, HTML and PHP | | | Record Set Paging with PHP (RSP) Categories : PHP, MySQL, Navigation, Databases, HTML and PHP | | | Alternating background color for HTML table rows Categories : PHP, Databases, MySQL, HTML and PHP | | | Database resultset navigation Categories : PHP, HTML and PHP, Databases, MySQL, Navigation | | | Editing the virtusertable and sendmail.cw via PHP3.0 and Mysql Categories : MySQL, HTML and PHP, PHP, Databases | | | This function will populate the options in a drop down HTML select list
in a form from a database query.
Categories : MySQL, General SQL, PHP, HTML and PHP, Databases | | | html split bar used to split in multiple pages a database result Categories : HTML and PHP, Databases, MySQL, PHP | | | This program will take data from a user via a web based form, validate it, show it
to the user for re-validation, and finally insert it into the database. Plenty of
sanity checking on the fields in the form.
Categories : MySQL, HTML and PHP, PHP, Complete Programs, Databases | |
|
|