August 18

So I like making life easier and all that for myself when coding….  So I decided a while back to recreate some of the tags I used to have in happy ClodFusion land into PHP land.  Granted I’m sure they aren’t as pretty or nifty, but they work so I’m happy.  The first one I made was for a textbox type input.

// print a single-line text box
function input_text($element_name, $element_label, $values, $maxLen = ”, $maxSize = ”, $juju=”) {
print ‘<dt style=width:”‘.$juju.’ !important”>
<label for=”‘ . $element_name . ‘”>’ . $element_label . ‘</label>
<input type=”text” name=”‘ . $element_name .'” id=”‘ . $element_name .'” maxlength=”‘. $maxLen .'” size=”‘. $maxSize. ‘”  value=”‘;
if ($values == ‘$_POST’){
print htmlentities($values[$element_name]) . ‘” /></dd>’;
print htmlentities($values) . ‘” /></dd>’;

So once the code is included somewhere with your functions you can call it and it makes a pretty little textbox for you.  I have the following in the style sheet so that the text boxes will align and all that.

dt label{
clear: both;
width: 10em;
padding: 0 0 .1em 0;
text-align: right;
dd input, dd select, dd textarea{
float: left;
width: 17em;
margin: .25em 0 .25em 0;
padding-left: .2em;

Usually to use it I will create a displayFom() function and stuff the form I want to use in it.

//example Form Funtion
function displayForm(){
print ‘<form method=”POST” action=”‘. $_SERVER[‘PHP_SELF’]. ‘ “>’;
//This is the form’s main fieldset
print “<fieldset>
<legend title=\”\”>Blah Form</legend>”;
print “<fieldset>
<legend title=\”Blah Information\”>Blah Information:</legend>”;
input_text(‘firstName’, ‘*First Name: ‘, $_POST[‘firstName’]);
input_text(‘lastName’, ‘*Last Name: ‘, $_POST[‘lastName’]);
print “</fieldset>”;
print “</fieldset>”;
input_submit(‘submit’,’Add this Volunteer’);

At that point if I want a single stage form I’ll write an if statement wondering if the submit has been triggered yet and check to make sure that my required fields are filled in.

//Here it checks to see if the form was submitted
if (isset($_POST[‘submit’])){
//Then it gets ready to check the required fields
//initialize the errors array for validation
$errors =array();
//Create a database record ID if you need one
$ID = uniqid(“”,true);
//replicate the blow code chunk and enter your required fields to either validate them or trigger the error message
if (empty($_POST[‘firstName’])){
$errors[] = “You forgot to enter the Volunteer’s First Name…”;
} else {
$firstName = trim($_POST[‘firstName’]);
if (empty($_POST[‘lastName’])){
$errors[] = “You forgot to enter the Volunteer’s Last Name…”;
} else {
$lastName = trim($_POST[‘lastName’]);
//if there aren’t any errors
if (empty($errors)){
// then connect to the database
//get ready to insert the record
$query = “INSERT INTO things(stuffID,
//Insert insert insert!
$result = mysql_query($query);
// If all goes well you should hit here and go on
if ($result) {
//Yay the database insert worked!
echo “<h3> Thank you!</h3>”;
echo “<p>”. $firstName . ” has been added</p>
<p><a href=\”/\”>Go back to the  Website</a></p>”;
} else {
//booooo the database insert didn’t work, so now show me how I messed up…
echo “<h3>Uh… Oh</h3><p>I couldn’t process your server information… </p>”;
echo “<p>Please let the webmistress know the following has happened: <br />” . mysql_error();
echo $query;
} else {
//If there are errors in the required fields, this reports the errors
echo “<h3>The following error(s) occurred:</h3>”;
foreach($errors as $msg) {
//Prints each error
echo “- $msg <br /> \n”;
//and then redisplays the form to be finished in again with the previously entered information
//if the submit button hasn’t been pushed then it goes on to display the initial form
} else {

That’s pretty much it… This just uses the one form field type.