保留文本字段中的值

the user fills in the text fileds of the form then clicks submit. then the data that has been entered will be shown. the user clicks Back to go back to the form...

the task:

how can i make the values in the text fields retain that the user has previously entered when the data failed to save or encountered an error so that the user doesn't have to fill in all the text fields again?

thanks.

I have this code to retain the values but i don't know if it's right or where to put it.

<form action="SSPage.php" method="post">
<input type="hidden" value="<?=$_POST['fname'];?>" />
<input type="hidden" value="<?=$_POST['lname'];?>" />
<input type="hidden" value="<?=$_POST['mname'];?>" />
<input type="submit" value="Back" />

here is my form code:

<body>

<form name="form1" method="post" action="">
  <table border="0" align="center" class="addPrincipalTable">
    <tr>
      <td width = "200" >&nbsp;<label>First Name  </label>
        <label for="textfield"></label></td>
      <td width = "180" align="right"><input type="text" name="fname" id="textfield" ></td>
    </tr>  
      <br />
    <tr>  
      <td width = "200" >&nbsp;<label>Middle Name  </label>
        <label for="textfield"></label></td>
      <td width = "180" align="right"><input type="text" name="mname" id="textfield"  ></td>
    </tr>  
      <br />
    <tr>  
      <td width = "200" >&nbsp;<label>Last Name  </label>
        <label for="textfield"></label></td>
      <td width = "180" align="right"><input type="text" name="lname" id="textfield"  ></td>
   </tr>
      <br />



<?php
require_once ('../include/config.php');
require_once ('../include/opendb.php');

echo "<tr>";
echo "<td width = 200>"."Group Name"."</td>";
$result = mysql_query("SELECT group_name FROM ss_group") or die(mysql_error());
$dropdown = "<select name=\"group\">
";
while($row = mysql_fetch_assoc($result)) {
$dropdown .= "
<option value='{$row['group_name']}'>{$row['group_name']}</option>";
}
$dropdown .= "
</select>";

echo "<td align='right'>".$dropdown."</td>";
echo "</tr>";
echo "</table>";
?>

 <table align="center" class="addPrincipalTable" border="0">
    <tr class="options_button" align="center">
    <div class = "cancel">
        <td align="center" width="384"> <input type="submit" name="insert" value="Insert" width="126" height="18" /> 
        <input type="submit" name="clear" value="Clear" onlick="document.form1.clear.value = '';"/> 
         <a href = "../SS/SSPage.php"><input type="submit" name="cancel" value="Cancel" /></a> </td>
    </tr>  
    </div>  
   </table>
</form>

</body>
</html>

<form action="SSPage.php" method="post">
<input type="hidden" value="<?=$_POST['fname'];?>" />
<input type="hidden" value="<?=$_POST['lname'];?>" />
<input type="hidden" value="<?=$_POST['mname'];?>" />
<input type="submit" value="Back" />


<?php
require_once ('../include/config.php');
require_once ('../include/opendb.php');

if (isset($_POST['insert']))
{

$first = $_POST['fname'];
$middle = $_POST['mname'];
$last = $_POST['lname'];
$group= $_POST['group'];


$result = mysql_query("SELECT * from staffing_specialist WHERE first_name LIKE '%$first%' AND middle_name LIKE '%$middle%'
AND last_name LIKE '%$last%'");
$row = mysql_fetch_array($result);
$numOfEntries = mysql_num_rows( $result);

if($numOfEntries >= 1){
    header ("location: include/insert_errorDuplicates.php");
    exit();
}

if(($first == NULL) || ($middle == NULL) || ($last==NULL)){

    header ("location:../include/insert_errorSS.php");
    exit();
}

$myquery = mysql_query("SELECT SS_group_ID FROM ss_group WHERE group_name LIKE '%$group%'");//get the group ID through        the item from combo box
$myresult = mysql_fetch_array($myquery);
$SS_group_ID = $myresult['SS_group_ID'];//the principal Id

$query = "INSERT INTO staffing_specialist(SS_group_ID,first_name,middle_name,last_name)
VALUES('$SS_group_ID','$first','$middle','$last')";

$result = mysql_query($query);

  if($result){
     header ("location:../include/insert_successSS.php");
   }
  else
    header ("location../include/insert_errorSS.php");
}

 else if (isset($_POST['clear'])){


 }
?>

Set the value attribute of the inputs to be the $_POST['...'] values if they are not empty.

<input type="text" name="fname" id="textfield" 
value="<?= array_key_exists('fname', $_REQUEST) ? $_REQUEST['fname'] : '' ?>">

I prefer to use jQuery / JavaScript to do form submission so that you don't need to leave the page.

Code in jQuery

$(document).ready(function(){  
$("form#submit").submit(function() {  
// we want to store the values from the form input box, then send via ajax below  
var fname     = $('#fname').attr('value');  
var lname     = $('#lname').attr('value');  
    $.ajax({  
        type: "POST",  
        url: "ajax.php",  
        data: "fname="+ fname +"& lname="+ lname,  
        success: function(){  
            $('form#submit').hide(function(){$('div.success').fadeIn();});  

        }  
    });  
return false;  
});  

});