WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
Internet Security Software
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Required form fields that pull from MySQL database
Categories : PHP, HTML and PHP, Databases, MySQL Update Picture
Erica Douglass
Date : Oct 16th 2000
Grade : 1 of 5 (graded 1 times)
Viewed : 12550
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Erica Douglass
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 
 

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



Record Set Paging with PHP (RSP)
Categories : PHP, MySQL, Navigation, Databases, HTML and PHP
A script to generate a report from a valid mysql connection. The user has to supply which fields he wants to display in table. All properties are changable.
Categories : PHP, PHP Classes, Databases, MySQL, HTML and PHP
A PHP useradmin for MySQL. Uploading is easy. The only thing you need is PHP and MySQL installed!
Categories : MySQL, Databases, PHP, HTML and PHP
Paginating the mySQL data
Categories : PHP, Algorithms, Databases, MySQL, HTML and PHP
Pull Down Surfing - Surf on Change
Categories : Java Script, MySQL, HTML and PHP, PHP, Databases
Using MySQL ENCODE / DECODE with PHP & HTML
Categories : PHP, HTML and PHP, Databases, MySQL
PHP and MySQL scripting for Muyltiple CheckBoxes
Categories : HTML and PHP, MySQL, Databases, 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
Message of the Day - Random Message (Needs MySQL!)
Categories : Databases, HTML and PHP, PHP, MySQL
dynamic table columns
Categories : PHP, HTML and PHP, Arrays, Databases, MySQL
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
Complex paging with no resultset limit
Categories : PHP, MySQL, Databases, Output Control, HTML and PHP
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