如果发现错误,则需要在单击“提交”按钮后保留文本框中的值

heartily regards from me I need to retain values in text boxes after submit button on click if found error. basically the thing I want, if user left any fields blank and press save button then error message pop up and without refreshing the page it takes back the user to the form where he just have to fill the fields that were left blank... below is my code

<html>
<form method="post" action="">
Enter Name  :   <input type="text" name="name" /><br/>
Enter Password  :   <input type="password" name="pass" /><br/>
<input type="submit" name="save" value="Save" />
</form>
</html>
php code
<?php
if (isset($_POST["save"]))
{
$name = $_POST["name"];
$pass = $_POST["pass"];
if (($name == '') && ($pass == '')) 
{
echo "Fields Must Be Filled...";
exit();
}
if ($name == '') {
echo "Enter Name...";
exit();
}
if ($pass == '') {
echo "Enter Password...";
exit();
}
else
{        
echo "Your name " . $name;
echo "<br/>";
echo "Your Password " . $pass;
}
}
?>

First, a little easier approach for your POST handling. You can do a

foreach( $_POST as $key => $value )
{
   ${$key} = $value;
   //If you need to database process the data, you can put mysql_escape_string( $value );
}

Now, all your POSTs will be in variables with the name of the field.

After processing them for errors and more, and you wish them to be in your form element's value. You can just use the variable

<input type="text" name="email" value="<?=$email?>">

Or, if you use the POST

<input type="text" name="email" value="<?=$_POST['email']?>">

Best regards. Jonas

Working code :-)

<?php
if (isset($_POST["save"]))
{
    //Run through all objects set in the POST array
    foreach( $_POST as $key => $value )
    {
        //Set a variable named the same as the input elements name, and with the value
        ${$key} = $value;
    }
}

    $error = false;
    if( empty($name) && empty($pass) ) 
    { 
        $error = true;
        $message = "Fields must be filled...";
    }
    elseif( empty($name) )
    { 
        $error = true;
        $message = "Enter name...";
    }
    elseif( empty($pass) )
    {
        $error = true;
        $message = "Enter Password...";
    }

    if( $error == true && isset($message) )
    {
        echo $message."<br><br>";
    }
    else
    {
        echo "Your name " . $name;
        echo "<br/>";
        echo "Your Password " . $pass;
    }
?>
<html><br><br>
<form method="post" action="">
Enter Name  :   <input type="text" name="name" value="<?=(isset($name) ? $name : "")?>"/><br/>
Enter Password  :   <input type="password" name="pass" value="<?=(isset($pass) ? $pass : "")?>" /><br/>
<input type="submit" name="save" value="Save" />
</form>
</html>

Enter Name : <input type="text" name="name" /><br/>

would be something like this

<?php 

$postvalue_name = (isset($_POST['name'])) ? $_POST['name'] : 'Fill in your name..'; 
?>

Enter Name  : <input type="text" name="name" value="<?php echo $postvalue_name; ?>" /><br/>

Just set the value of your input fields to match the $_POST arguments. Your form would be..

<html>
<form method="post" action="">
Enter Name  :   <input type="text" name="name" value="<?php echo $_POST['name']; ?>" /><br/>
Enter Password  :   <input type="password" name="pass" value="<?php echo $_POST['pass']; ?>" /><br/>
<input type="submit" name="save" value="Save" />
</form>
</html>