Here's a different way to generate forms using PHP, and to take care of all the required field validation too. It might help someone:

array(
"type"=>"select",
// attributes are an associative array
"attributes"=>array(
"mr"=>"Mr.",
"ms"=>"Ms.",
"miss"=>"Miss.",
"mrs"=>"Mrs.",
),
),
"first_name"=>array(
"type"=>"text",
"size"=>40,
"required"=>true,
"onclick"=>"alert('test');",
"style"=>"border:2px solid #CCCCCC; padding:4px; font-size:15px;",
),
"last_name"=>array(
"type"=>"text",
"size"=>70,
"required"=>true,
),
);

$errors=array();

if($_REQUEST['save']){
// do sql here...
$sql = "INSERT INTO foo SET insert_date=NOW()";
foreach($elements as $element_name => $el){
if($el['required']&&!$_REQUEST[$element_name]){
$errors[]="Required field missing: $element_name";
}else{
$sql .= ", `$element_name` = '".addslashes($_REQUEST[$element_name])."'";
}
}
if(!$errors){
// call sql:
// $res = query($sql,$db);
// $member_id= mysql_insert_id($db);
// redirect after post back
header("Location: example.php?member_id=$member_id");
exit;
}
}

global $elements;
function fb($elem){
global $elements;
$el = $elements[$elem];
if(!$el['name'])$el['name']=$elem;
if(!$el['value']&&$_REQUEST[$el['name']])$el['value']=$_REQUEST[$el['name']];
switch($el['type']){
case "textarea":
?>
$val){ if($key=="value")continue; ?> ="" >

$val){ ?> ="" >

$val){ if($key=="attributes")continue; ?> ="" >
- select -
$val) {
?>
selected >














Salutation:

First Name:

Last Name:





First name and last name are required fields. Try clicking save without filling them in.









View cod

in reference to: required field validator in php - Google Search (view on Google Sidewiki)

Comments

Popular posts from this blog

8 Best CMSs for building a well structured social networking website

Manor House - Telscombe Village