Server side validation


I want to have a good method of validating forms server side, i was looking into the following example:

So i tried having a go, but couldn’t get the hang of it. I tried using this code:

    if((isset($_POST['billingFirstName']) && $_POST['billingFirstName'] == "")
        || (isset($_POST['billingSurname']) && $_POST['billingSurname'] == "")
        || (isset($_POST['billingAddress1']) && $_POST['billingAddress1'] == "")
        || (isset($_POST['billingCity']) && $_POST['billingCity'] == "")
        || (isset($_POST['billingPostcode']) && $_POST['billingPostcode'] == "")
        || (isset($_POST['deliveryFirstName']) && $_POST['deliveryFirstName'] == "")
        || (isset($_POST['deliverySurname']) && $_POST['deliverySurname'] == "")
        || (isset($_POST['deliveryAddress1']) && $_POST['deliveryAddress1'] == "")
        || (isset($_POST['deliveryCity']) && $_POST['deliveryCity'] == "")
        || (isset($_POST['deliveryPostcode']) && $_POST['deliveryPostcode'] == "")):

        $blankVariables = array();
        $blankfields = true;

        foreach($_POST as $key=>$post):
            if($post == '' || $post == "Please select"):
                array_push($blankVariables, $key);
            $blankfields = false;

And then tested the code using this:

            <div style="padding:5px; border:1px solid #990000; color: #990000; margin: 10px 0 0 0">
                Please fill out all fields marked *. <br/>
                <?foreach($blankVariables as $var):?>
                    <?if($var != 'billingAddress2' && $var != 'deliveryAddress2' && $var != 'phone_eve'):?>
                were left blank.

It works fine, BUT the problem is that it display the error message like this:

Now if i take one input element just to show you how they are:

<input type="text" name="billingSurname" maxlength="45" value="<?if(isset($_POST['billingSurname'])):
echo $_POST['billingSurname'];
echo $_SESSION['billingSurname'];
endif; ?>"/>

And now look back at the error message, it gives the input name in the error:


I want to be able to give it a value, so if the missing field is billingSurname, it should display:

Billing Surname

Instead of billingSurname…

Is this possible? How can i achieve this by changing bits of my code?


Thanks, that worked perfectly!


Yup, $var =


Or you could combine it all together:

<?= str_replace(
     ' ',
         ' $1',

Amazing! I don’t understand how thats worked, what does this line actually do?

<?=ucfirst(preg_replace('~([A-Z])~', ' $1', $var));?>,

I’ve used preg_replace before but not in this context…

Also one of the fields is called Phone_day, can i replace the underscore with a space?


            <div style="padding:5px; border:1px solid #990000; color: #990000; margin: 10px 0 0 0">
                Please fill out all fields marked *. <br/>
                <?foreach($blankVariables as $var):?>
                    <?if($var != 'billingAddress2' && $var != 'deliveryAddress2' && $var != 'phone_eve'):?>
                        <?=ucfirst(preg_replace('~([A-Z])~', ' $1', $var));?>,
                were left blank.

Should do what you want, or am I missing something?

As long as you keep the convention of billingMethod you can do the following

echo ucfirst(preg_replace('~([A-Z])~', ' $1', $str)); // Billing Method

echo ucfirst(preg_replace('~([A-Z])~', ' $1', $str)); // Delivery First Name


Sorry, i’m not quite sure i’m following:

So with what you have suggested, would i be able to display the error message like this:

Please fill out all fields marked *.
Email, Daytime Phone, Billing First Name, Billing Surname,
Billing Address 1, Billing City, Billing Postcode, Delivery
First Name, Delivery Surname, Delivery Address 1,
Delivery City, Delivery Postcode, Country, were left blank.

Instead of what you can see in the image i provided?

Not all the input’s start with “billing”, some are “delivery”, one is email etc… So they are all differently named…


ucfirst returns the input string with the first character capitalized.
ucfirst(‘someText’) = ‘SomeText’;

preg_replace(‘~([A-Z])~’, ’ $1’, $var); replaces every occurrence of a capital with a space proceeded by the matched capital (someText => some Text)

So if we combine the two: ‘someText’ => ‘Some Text’ :slight_smile:

As for replacing an underscore with a string, str_replace :cool:

Ah yes str_replace, i am trying to use it like this:

ucfirst(preg_replace('~([A-Z])~', ' $1', $var));
str_replace('_', ' ', $var);

But it doesn’t seem to work…

Am i missing something?