Here is a function that is E_ALL safe so you don't need to keep repeating code:
PHP Code:
<?php
function sticky($key, $type = NULL, $value = NULL)
{
if(empty($_POST[$key])) return FALSE;
if($type != ('radio' or 'checkbox' or 'option')) return $_POST[$key];
if(($type == 'radio') and ($value == $_POST[$key])) return ' checked="checked"';
if(($type == 'checkbox') and (($value == $_POST[$key]) or (is_array($_POST[$key])
and in_array($value, $_POST[$key])))) return ' checked="checked"';
if(($type == 'option') and ($value == $_POST[$key])) return ' selected="selected"';
return FALSE;
}
?>
<?php
// the following is solely for demonstration purposes
?>
<form action="<?php echo $PHP_SELF; ?>" method="post">
<input type="text" name="field1" value="<?php echo sticky('field1'); ?>"><br><br>
<input type="radio" name="field2" value="yes"<?php echo sticky('field2', 'radio', 'yes'); ?>>Yes
<input type="radio" name="field2" value="no"<?php echo sticky('field2', 'radio', 'no'); ?>>No<br><br>
<input type="checkbox" name="field3[]" value="optionA"<?php echo sticky('field3', 'checkbox', 'optionA'); ?>>Option A
<input type="checkbox" name="field3[]" value="optionB"<?php echo sticky('field3', 'checkbox', 'optionB'); ?>>Option B<br><br>
<select name="field4" size="1">
<option value="A"<?php echo sticky('field4', 'option', 'A'); ?>>A</option>
<option value="B"<?php echo sticky('field4', 'option', 'B'); ?>>B</option>
</select><br><br>
<input type="submit" name="submit" value="Roll">
</form>
Bookmarks