I need your help with some form/php code:
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.