php表单保存输入[关闭]

I need your help with some form/php code:

  1. 2Fields first for name 2nd for fam.name
  2. If the "User" fill both fields its echo what he filled in
  3. If the "User" dont fill any field he get form back with an error message
  4. Same if he just fill only 1 of the 2 fields
  5. My question is if the "User" just fill 1 field and send it (submit) i want that he gets the error message like now but also save his input (the date he filled in the field) so he only has to fill the other field and not both again.
  6. I tried it allready with session_start(); and stuff but failed.

    <?php
    
    $errormessage="";
    
    //check if 1 of 2 fields is empty
    if(empty($_POST['vorname'])){
    $errormessage=$errormessage." First field ,";
    }
    
    if(empty($_POST['nachname'])){
    $errormessage=$errormessage." Second field  ,";
    }
    
    //Submit starts
    if (isset($_POST['go'])) {
    
        if (!empty($_POST['vorname'])&& !empty($_POST['nachname'])){
            echo "First field :  ".$_POST["vorname"]." ";       
            echo " 2nd field:  ".$_POST["nachname"];
          }//end empty
    
        //ELse if inputs empty
        if (!empty($errormessage)) {  ?>
        <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
            <input type="text" name="vorname" />
            <input type="text" name="nachname"  />
            <input type="submit" name="go" />
            <?php echo "error:  ".$errormessage; ?>
        </form>
    <?php  } //ende  if errormessage
    
        }//ende if isset go
    
    else {//start side ?>
    <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
        <input type="text" name="vorname" />
        <input type="text" name="nachname" />
        <input type="submit" name="go" />
    </form> 
    <?php}  //end else?> 
    


If user specified some info only for one field and send it, why don't you paste correct filled data from $_POST var into value attribute of one of yours inputs:

<input type="text" name="vorname" value="<?php echo $correct_data ?>"/>

and show additional error message, like you do?

I mean, you can save old $_POST['vorname'] and $_POST['nachname'] into two extra vars, if one of the $_POST fields is empty, one of this var is empty, then add this vars in value attrubutes of your inputs in html form:

$errormessage = "";
$old_vorname = empty($_POST['vorname']) ? "" : $_POST['vorname'];
$old_nachname = empty($_POST['nachname']) ? "" : $_POST['nachname'];
....
//ELse if inputs empty
if (!empty($errormessage)) {  ?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
    <input type="text" name="vorname" value="<?php echo $old_vorname; ?>"/>
    <input type="text" name="nachname" value="<?php echo $old_nachname; ?>"/>
    <input type="submit" name="go" />
    <?php echo "error:  ".$errormessage; ?>
</form>

Value attribute of html input element contains it actual content, you can see on webpage or edit.