Form Programming
<p>I'm coding form for new users to sign up, but I can't help think my code just looks messy and its now getting difficult for me to manage.</p>

<p>I still have other things to do to the form, additional sanitization and checks. But adding more code to my form is going to get really messy.</p>

<pre><code>&lt;?php
session_start();

require_once('lib/recaptchalib.php');

// Recaptcha credentials
$publickey = "6LdcHMgSAAAAANaGW3Idbc2WRpkSpSFzrIVLlKh8";
$privatekey = "6LdcHMgSAAAAAMaNNeW1W7HGxwbq30q0amRPM_SR";

// Captcha responses
$resp = null;
$error = null;

$notifications = array();

$valid_form_fields = array(
    'first_name',
    'last_name',
    'contact_number',
    'email',
    'signup',
    'recaptcha_challenge_field',
    'recaptcha_response_field',
);

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

    foreach($_POST as $key =&gt; $val) {
        if(!in_array($key, $valid_form_fields)) {
            header('Location: signup.php');
            exit;
        }
    }

    if(empty($_POST['first_name'])) {
        $notifications[] = 'Please provide your first name';
    } else {
        if(strlen($_POST['first_name']) &gt; 20) {
            $notifications[] = 'First name is too long. 20 characters max.';
        }

        if(!ctype_alnum($_POST['first_name'])) {
            $notifications[] = 'First name contains illegal characters.';
        }
    }

    if(empty($_POST['last_name'])) {
        $notifications[] = 'Please provide your last name';
    } else {
        if(strlen($_POST['last_name']) &gt; 20) {
            $notifications[] = 'Last name is too long. 20 characters max. Letters and numbers only.';
        }

        if(!ctype_alnum($_POST['last_name'])) {
            $notifications[] = 'Last name contains illegal characters. Letters and numbers only.';
        }
    }

    if(empty($_POST['contact_number'])) {
        $notifications[] = 'Please provide a contact number';
    } else {
        if(strlen($_POST['contact_number']) &gt; 15) {
            $notifications[] = 'Contact number is too long. 15 characters max.';
        }

        if(!ctype_digit($_POST['contact_number'])) {
            $notifications[] = 'Contact number must contain digits only.';
        }
    }

    if(empty($_POST['email'])) {
        $notifications[] = 'Please provide a valid email address';
    } else {
        if(strlen($_POST['email']) &gt; 50) {
            $notifications[] = 'Email address is too long. 50 characters max.';
        }
    }

    if(empty($_POST['recaptcha_response_field'])) {
        $notifications[] = 'Please enter the code from the image below to continue registration.';
    }

    if ($_POST["recaptcha_response_field"] &amp;&amp; $notifications == 0) {
        $resp = recaptcha_check_answer (
            $privatekey,
            $_SERVER["REMOTE_ADDR"],
            $_POST["recaptcha_challenge_field"],
            $_POST["recaptcha_response_field"]);

            if ($resp-&gt;is_valid) {
                $hash = md5(rand(0,1000) . time());
                $password = substr(md5(time()), 0, 8);

                try {
                    $dbh = new PDO('mysql:dbname=lf_database;host=127.0.0.1', 'root', 'root');
                    $sql = "INSERT INTO users (id, firstname, lastname, phone, hash, email, password, verified, active) values (null, :firstname, :lastname, :phone, :hash, :email, :password, null, null)";
                    $sth = $dbh-&gt;prepare($sql);
                    $sth-&gt;execute(array(
                        ':firstname'    =&gt; $_POST['firstname'],
                        ':lastname'     =&gt; $_POST['lastname'],
                        ':phone'        =&gt; $_POST['contact_number'],
                        ':hash'         =&gt; $hash,
                        ':email'        =&gt; $_POST['email'],
                        ':password'     =&gt; $password, 
                    ));

                    $notifications[] = "Your account has been created. Please check your email for verification.";


                } catch (PDOException $e) {
                    echo 'We\'re having database issues at the moment. Don\'t worry, we\'re getting it sorted!';
                }
            } else {
                $notifications[] = 'The code you entered does not match. Please try again.';
            }
    }

}
?&gt;
</code></pre>
