I've been experimenting a form class where the output gets printed through a template. In essence I've got most of it working my problem comes however from the completed validated form. I need to create an arguement that can use the return form the class to submit to a db and print the input to the page (that won't actually be a problem). It's simply getting the final argument working. This will probably make more sense with the scripts so:
the called page adduser.php
<?php
//include external documents
include('includes/form.inc');
include('includes/dbconnect.inc');
include ('templates/template.inc');
//crete form object
$form = new form
(array
(array('label'=>"orgnaisation", 'name'=>"orgname", 'type'=>"text", 'value'=>"$orgname", 'required'=>"true"),
array('label'=>"address one", 'name'=>"addone", 'type'=>"text", 'value'=>"$addone", 'required'=>"true"),
array('label'=>"address two", 'name'=>"addtwo", 'type'=>"text", 'value'=>"$addtwo", 'required'=>""),
array('label'=>"county", 'name'=>"county", 'type'=>"text", 'value'=>"$county", 'required'=>""),
array('label'=>"postcode", 'name'=>"postcode", 'type'=>"text", 'value'=>"$postcode", 'required'=>""),
array('label'=>"Phone #", 'name'=>"phonenum", 'type'=>"text", 'value'=>"$phonenum", 'required'=>""),
array('label'=>"Fax #", 'name'=>"faxnum", 'type'=>"text", 'value'=>"$faxnum", 'required'=>""),
array('label'=>"Email", 'name'=>"email", 'type'=>"text", 'value'=>"$email", 'required'=>""),
array('label'=>"URL", 'name'=>"url", 'type'=>"text", 'value'=>"$url", 'required'=>""),
array('label'=>"VAT #", 'name'=>"vatnum", 'type'=>"text", 'value'=>"$vatnum", 'required'=>""),
array('label'=>"Company #", 'name'=>"url", 'type'=>"text", 'value'=>"$url", 'required'=>""),
array('label'=>"Account Status", 'name'=>"accountstatus", 'type'=>"checkbox", 'value'=>"$accountstatus", 'required'=>""),
array('label'=>"Live", 'name'=>"live", 'type'=>"checkbox", 'value'=>"$live", 'required'=>""),
)
);
//form header
$containerhead=
"<div id=\"formcage\">
<form action=\"$php_self?action=\" method=\"post\">
";
//form footer
$containerend="
<div class=\"formrow\">
<span class=\"input_cage\">
<input class=\"button\" type=\"submit\" value=\"submit\">
</span>
</div>
</form>
";
//if there's been no submission yet
if (!isset($action))
{
//initiate template
$t=new template("templates");
$t->set_file("input", "templates/mask.php");
//set the vars
$t->set_var("content", $form->output());
$t->set_var("containerhead", $containerhead);
$t->set_var("containerend", $containerend);
//print output
$t->pparse("output", "input");
}
//once it's been submitted
else if (isset($action))
{
//initiate template
$t=new template("templates");
$t->set_file("input", "templates/mask.php");
//set the vars
$t->set_var("content", $form->output());
$t->set_var("containerhead", $containerhead);
$t->set_var("containerend", $containerend);
//print output
$t->pparse("output", "input");
}
else //something else with I can't sort out, should be something about isset$action and there are no
{
//go submit and print input to page
}
?>
the form class form.inc
<?php
include "includes/construct.php";
include "includes/validation.php";
class form
{
var $form_array = array();
function form ($field)
{
$this->field = $field;
$this->action = $action;
}
//generated html to print ot screen
function output()
{
foreach ($this->field as $field)
{
//required validation
if (($field[required] == "true")&&($field[value] == ""))
{
$submit = warning($field);
if ($submit)
{
$html.=$submit;
}
}
//email validation
if ($field[custom] == "email")
{
$email = email($field);
if ($email)
{
$html.=$email;
}
}
//print out the rest
else
{
$html.=type($field);
}
}
//return the html string
return $html;
//end of output function
}
//end of form class
}
?>
functions to build the fields construct.php
<?php
//functions to build the fields, probably going to be a switch statements when I ge to it
function type($field)
{
if ($field[type] == text)
{
$html="
<div class=\"formrow\">
<span class=\"label\">$field[label]</span>
<input class=\"field\" type=\"$field[type]\" name=\"$field[name]\" value=\"$field[value]\">
</div>";
return $html;
}
else if ($field[type] == password)
{
$html="
<div class=\"formrow\">
<span class=\"label\">$field[label]</span>
<input class=\"field\" type=\"$field[type]\" name=\"$field[name]\" value=\"\">
</div>";
return $html;
}
else if ($field[type] == textarea)
{
$html.="
<div class=\"form\">
<span class=\"label\">$field[label]</span>
<span><$field[type] name=\"$field[name]\">$field[value]</textarea></span>
</div>";
return $html;
}
else if ($field[type] == select)
{
$selected = $field[value];
$html.="
<div class=\"formrow\">
<span class=\"label\">$field[label]</span>
<select name=\"$field[name]\">";
//create drop down list from org_class table
//first make the selection
$html.= select($field, $selected);
$html.="
</select>
</div>";
return $html;
}
if ($field[type] == checkbox)
{
if ($field[value] == "")
{
}
else if ($field[value] != "")
{
$checked = checked;
}
$html.="
<div class=\"formrow\">
<span class=\"label\">$field[label]</span>
<input class=\"field\" type=\"$field[type]\" name=\"$field[name]\" value=\"1\" $checked>
</div>";
return $html;
}
//end of type function
}
?>
the validation functions validation.php
<?php
//once form is submited run through checks
//if ($this->action == 2)
//{
//eamil validation function
function email($field)
{
//basic valiation of mail address
if (!ereg('^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+'. '@'. '[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.' . '[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$', $field[value]))
{
$html.="
Required
";
return $html;
}
//end of email function
}
//required field warning
function warning($field)
{
$html.="
Required
";
return $html;
//end of warning function
}
?>
the template mask mask.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
{title}
</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div align="left"
style="width:100%; height:100%; background-color:white;">
<div
style="position:absolute; top:20px; width:300px; background-color:white;">
{containerhead}
{content}
{containerend}
</div>
</div>
</body>
so that's it. Any help woul dbe greatly apprechiated since it's been bugging me for weeks now.