I think
DaButcher has a very good idea. I would like the opportunity to embellish on it....
Given the following HTML, from either a static file or a script:
Code:
<html><body>
<form method="post" action="test_foo.php">
<input type="text" name="formfield1"><br>
<input type="text" name="formfield2"><br>
<input type="text" name="formfield3"><br>
<input type="submit">
</form>
</body></html>
and the script test_foo.php:
Code:
<?php
$default_values = array
(
'formfield1' => 'defaultvalue1',
'formfield2' => 'defaultvalue2',
'formfield3' => 'defaultvalue3',
);
function wash ()
{
global $default_values;
foreach ($default_values as $variable => $value)
{
if (!isset($_POST[$variable]) || $_POST[$variable] == '')
{
$GLOBALS[$variable] = $value;
}
else
{
$GLOBALS[$variable] = $_POST[$variable];
}
}
}
wash();
print '<html><body><pre>';
print $formfield1 . "\r\n";
print $formfield2 . "\r\n";
print $formfield3 . "\r\n";
print '</pre></body></html>';
?>
The function wash() will instantiate, in the global address space, all the fields from the form with defaults in place if no values are submitted for a field. If, for example, I enter [tt]z[/tt] into the second field and submit, the output of test_foo.php is:
[tt]defaultvalue1
z
defaultvalue3[/tt]
It still uses if-statements, but they are all neatly bound up in a function, as
DaButher suggested. All you need to is modify the associative array $default_values so that the fieldname is the key and its default value is the value.
I don't particularly care for creating the extra variables, as $_POST already has the values. (I only use references to the elements of $_POST in my code -- I figure 6 months from now I may not remember how a value got in $foo, but $_POST['foo'] is self-documenting.) But if your style is to create the extra variables, this might be what you're looking for.
Want the best answers?
Ask the best questions! TANSTAAFL!