Checking form input data on submit with pure PHP I have some experience with PHP but I have never even try to do this wit pure PHP, but now a friend of mine asked me to help him with this task so I sat down and write some code. What I'm asking is for opinion if this is the right way to do this when you want to use only PHP and is there anything I can change to make the code better. Besides that I think the code is working at least with the few test I made with it.

Here it is:

&lt;?php
session_start();

// define variables and initialize with empty values
    $name = $address = $email = "";
    $nameErr = $addrErr = $emailErr = "";
    $_SESSION["name"] = $_SESSION["address"] = $_SESSION["email"] = "";
    $_SESSION["first_page"] = false;


    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        if (empty($_POST["name"])) {
            $nameErr = "Missing";
        }
        else {
            $_SESSION["name"] = $_POST["name"];
            $name = $_POST["name"];
        }

        if (empty($_POST["address"])) {
            $addrErr = "Missing";
        }
        else {
            $_SESSION["address"] = $_POST["address"];
            $address = $_POST["address"];
        }

        if (empty($_POST["email"]))  {
            $emailErr = "Missing";
        }
        else {
            $_SESSION["email"] = $_POST["email"];
            $email = $_POST["email"];
        }
    }
    if ($_SESSION["name"] != "" &amp;&amp; $_SESSION["address"] != "" &amp;&amp; $_SESSION["email"] != "") {
    $_SESSION["first_page"] = true;
    header('Location: http://localhost/formProcessing2.php');
    //echo $_SESSION["name"]. " " .$_SESSION["address"]. " " .$_SESSION["email"];
    }
?&gt;
&lt;DCTYPE! html&gt;
&lt;head&gt;
&lt;style&gt;
.error {
    color: #FF0000;
}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form method="POST" action="&lt;?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?&gt;"&gt;
Name &lt;input type="text" name="name" value="&lt;?php echo htmlspecialchars($name);?&gt;"&gt;
&lt;span class="error"&gt;&lt;?php echo $nameErr;?&gt;&lt;/span&gt;
&lt;br /&gt;
Address &lt;input type="text" name="address" value="&lt;?php echo htmlspecialchars($address);?&gt;"&gt;
&lt;span class="error"&gt;&lt;?php echo $addrErr;?&gt;&lt;/span&gt;
&lt;br /&gt;
Email &lt;input type="text" name="email" value="&lt;?php echo htmlspecialchars($email);?&gt;"&gt;
&lt;span class="error"&gt;&lt;?php echo $emailErr;?&gt;&lt;/span&gt;
&lt;br /&gt;
&lt;input type="submit" name="submit" value="Submit"&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
It's incredibly repetitive.  Repetitive code is hard to maintain because you have to apply the same change in many places.  It's also hard to see at a glance where the similarities and differences lie.

Here is how it could be rewritten:

&lt;?php
    $params = array('name', 'address', 'email');

    session_start();
    $_SESSION['first_page'] = true;

    foreach ($params as $p) {
        $$p = $_SESSION[$p] = '';
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            if (empty($_POST[$p])) {
                $errorVarName = "${p}Error";
                $$errorVarName = 'Missing';
            } else {
                $$p = $_SESSION[$p] = $_POST[$p];
            }
        }
        if ($_SESSION[$p] == '') {
            $_SESSION['first_page'] = false;
        }
    }
    if ($_SESSION['first_page'])) {
        # None of the expected params is an empty string
        header('Location: http://localhost/formProcessing2.php');
    }
?&gt;


To go one step further, I would suggest using $val[$p] and $err[$p] instead of $$p and $$errorVarName, respectively.  That's because variably named variable names are icky, and smell vaguely of the stink associated with register_globals.  An additional benefit of using an associative array is that you can pass all of the processed parameter values as a single argument to a function, which you can't do if they are individual variables.  The template would need to be modified to say

Name &lt;input type="text" name="name" value="&lt;?php echo htmlspecialchars($val['name']);?&gt;"&gt;
&lt;span class="error"&gt;&lt;?php echo $err['name'];?&gt;&lt;/span&gt;