Thursday, October 27, 2005

Form Handling Tips

You probably know that PHP is great for handling forms. But what about all those form variables that you need to catch? That can be a pain, especially when there are a lot of them - or they change from time to time.

First, remember that variables can come into a PHP script in two (or more) different ways. With forms, they usually come in via the $_POST array. Older and less secure PHP installations had this special setting 'register_globals' set to 'on' which let your variables appear in the script without any effort. But that's pretty much one these days because it just isn't safe.

So we're back to the $_POST array.

Here's a clever little snippet of code I use all the time to make it easy to get all of the $_POST variables turned into local variables for my scripts. There are two parts to this process.

Part One - create an array of the form field names. Here's an example:

$inputs = array("id","type","button1","button2","fname");

You can add or remove field names from this array as needed. That means not having to write a bunch of $variable = $_POST[field_name]; lines over and over.

Part Two - turn every element in the above array into a local variable. This is the realy clever bit because it doesn't care what the field names are, how many there are, etc. Write it once and forget about it.

foreach ($inputs as $input) {
$$input = $_POST[$input];
}

Here's how it works: You're starting with an array ($inputs) with a bunch of field names. You loop through the array and look at each one as $input. Using $input as the index into the $_POST array gives you the field name. For example, if you have a field called 'email', it's stored as $_POST[email].

Then you put the $ in front of it to make it a local variable at the same time you're getting it from the $_POST array. Since the field was called 'email', you get a local variable called $email - which has the value stored in $_POST[email].

Presto! Every field in the form becomes a local variable. What you do with them after that is entirely up to you.

There's a similar thing you can do if you're getting 'key=value' pairs from a query string. Obviously, you want the key to become the name of a local variable holding the value that was passed. Here's the code for that:

$qs = split("&", $_SERVER["QUERY_STRING"]);
foreach ($qs as $input) {
$temp = split("=", $input);
$$temp[0] = $temp[1];
}

This may look a bit more complicated but it really isn't. Just follow the steps it performs and you'll see it's actually quite simple.

Step One: The query string itself (everything in a URL after the ?) is passed to your script automatically in the global array $_SERVER. You find it in there by asking for $_SERVER["QUERY_STRING"]. Whenever there is more than one key=value pair in a query string, they are separated by an ampersand (&).

Step Two: You break the query string into key=value pairs by splitting it with the split() function wherever there's an &. You save the results in an array that I named $qs (you can call it whatever you like).

Step Three: Loop through the array using foreach() like we did before. Each $input you pull out of the array will be a key=value pair = and the '=' sign will be in it. Split the pair on the '=' sign and you've got a list or array ($temp) containing (key,value). Since $temp[0] is the key, make it a variable by adding another $ sign in front of it. Since $temp[1] is the value, assign that value to the variable you just created.

That's it! all your key=value pairs have been extracted and turned into local variables. Thinking back for a second, if you had a query string like this:

?email=this@that.com&id=42

You'd end up with:

$email = 'this@that.com'; and $id = 42;

Come on back next week and I'll reveal another snippet from my bag of tricks.

-- Kaspar the friendly geek