I have been working on an application which uses $_GET arguments to build forms for user data entry. I have made extensive use of the switch statement, and while everything works just fine, I was wondering whether there might be more streamlined ways to do what I am trying to do. I am always looking out for best practices, etc.
I use a function, build_form(), to produce the form page. The build_form() function works out what elements are in the form by iterating through an array passed as an argument:
function build_form($form_type, $elements, $db) {
// The $form_type variable tells us which kind of form we are making,
// while the $elements array includes the specific elements of the
// form. $db is optional, in case we need to access the database.
?>
<form action ="<?php print($_SERVER[PHP_SELF]) ?>" method ="POST">
<input type="hidden" name="form_type" value="<?php print($form_type) ?>"><p>
<table>
<?php
foreach ($elements as $value) { // iterate through $elements to build the form
echo "<tr>\n<td align=\"right\">\n";
print (ucfirst($value));
echo ': </td><td>';
switch ($value) { // we do something a little different for each element type
case 'name':
case 'description':
case 'email':
case 'shots':
case 'score':
echo "<input type=\"text\" name=\"$value\"><p>\n";
break; // normal text cases
// some elements get special treatment: a drop-down menu
case 'shooter':
case 'course':
case 'class':
case 'club':
case 'league':
case 'target':
case 'target_type':
drop_menu($value, $db);
break; // cases fpr drop-downs
} // end of switch on $value
echo "</td>\n</tr>\n";
} // end of foreach on $elements
echo "<tr>\n<td align=\"right\" colspan=\"2\">\n";
echo "<input type=\"submit\" value=\"Submit\"><p>";
echo "</td>\n</tr>\n</table>\n</form>\n";
} // end of function build_form()
This works fine, and for now I'm not sure it needs any improvement. The question is, how do I build the form element array ($elements in the function, $form_elements in the main program)?
Here's how I am doing it now:
$form_elements = array('name');
switch ($_GET['item']) {
case 'club':
$form_elements[] = 'description';
$form_elements[] = 'email';
$form_elements[] = 'league';
break;
case 'league':
$form_elements[] = 'description';
$form_elements[] = 'email';
break;
case 'score':
$form_elements[0] = 'score';
$form_elements[] = 'shooter';
$form_elements[] = 'course';
$form_elements[] = 'class';
break;
case 'shooter':
$form_elements[] = 'email';
$form_elements[] = 'club';
break;
case 'course':
$form_elements[] = 'description';
$form_elements[] = 'target';
$form_elements[] = 'shots';
break;
case 'target':
$form_elements[] = 'description';
$form_elements[] = 'target_type';
break;
case 'class':
$form_elements[] = 'description';
break;
default:
html_footer(__FILE__, "Can't recognize Item.");
} // end of switch on $_GET['item']
build_form($_GET['item'], $form_elements, $connection);
As you can see, there will be a different array depending on what $_GET['item'] is. Again, this works just fine, but I can't help wondering whether there's a more streamlined approach. I am somewhat disturbed that I have so many entries of "$form_elements[] = 'description'", for example.
So is there a cleaner way to do this?
Thanks.