This is generally easy: just set the value of the fields based on the variable itself, e.g.:
echo "<input type=\"text\" name=\"test\" value=\"$test\">";
The first time the form is loaded, $test will not be defined, and the field will be empty. But after a POST, it will exist, so if you decide to resdisplay the form the field will have its previous contents.