«
»

HTML Form Tricks Every CodeMonkey Should Know

I just recently completed a fun data warehouse project that involved, among other things, providing a way to edit stored data via HTML forms. For all you codemonkeys out there, I thought I’d share a couple of cool shortcuts that I use to make form coding much easier.

Step 1: The HTML Form

Before we get to the tricks, we must start with the form. Here is a sample one we can use:html form tricks - first, the form code
A typical (if decidedly Yankee-oriented) form, and one that would (under other circumstances) require some styling to make it more presentable. Anyway, this one is set up to present a series of drop-down choices for positional favorites, with a Save button that sends the selected choices via POST to TEST.PHP for processing. The output looks like this:

html form trick - the form output

Trick #1: Testing the Form Output

Granted, this form is pretty simple with only five variables. But what if it were much bigger? What if there were lots of other choices, hidden fields, and other complexities? Well, what I usually do is to redirect the output to a special script that lets me monitor the values passed to it. So, instead of using action=”test.php” you’d use action=”magicscript.php” in your <form> statement. Check out this magicscript.php code:

<?php
session_start();
echo “Here are all the defined session variables<br /><br />”;
foreach ($_SESSION as $field => $value)
{
echo “$field = $value<br>”;
}
echo “Here are all the defined GET variables<br /><br />”;
foreach ($_GET as $field => $value)
{
echo “$field = $value<br>”;
}
echo “Here are all the defined POST variables<br /><br />”;
foreach ($_POST as $field => $value)
{
echo “$field = $value<br />”;
}
?>

So what does this magic script do? It runs three FOREACH loops to display all the¬†SESSION variables, GET variables, and POST variables passed to the script. It’s a great catch-all for testing your website as you work with those kinds of variables – you make your selections on your form, then use the magic script to make sure those choices come through like you expect. The output from the script looks like this:

html form trick - the script output(So I’m a child of the 70′s… sue me.)

For this website, we’re only using POST for now. But you can see how a more complex website using SESSION variables for security and GET or POST forms for data input, this could be pretty useful.

Trick #2: Quickly Accessing the Form Data

As I said, this form is pretty simple – unlike most forms we’ll encounter in the wild. For the actual processing script, in order to use these form variables we’d have to have a series of assignment statements like so:

<?php
$catcher = $_POST["catcher"];
$firstbase = $_POST["firstbase"];
$secondbase = $_POST["secondbase"];
$shortstop = $_POST["shortstop"];
$thirdbase = $_POST["thirdbase"];
?>

Again, if you had a really long form this could get very tedious, and potential result in typos or missed fields. So here’s another cool trick…

Remember that code we used above? Add a second ‘$’ to $field in the magicscript code above, and insert that into your processing script like so:

<?php
foreach ($_POST as $field => $value)
{
$$field = $value;
}
?>

This will automatically load all your POST variables using the variable names from your form. All the variables loaded with little code, without typos and without missing any. What could be easier?

Working With HTML Forms, Redux

Forms are a powerful tool for gathering and manipulating data, and can get pretty hairy sometimes. Use these tricks to help your testing and code verification, and may your code always work flawlessly.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>