Hi,
I want to have a good method of validating forms server side, i was looking into the following example:
http://tuxradar.com/practicalphp/7/7/3
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);
endif;
endforeach;
else:
$blankfields = false;
endif;
And then tested the code using this:
<?
if($blankfields):?>
<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'):?>
<?=$var;?>,
<?endif;?>
<?endforeach;?>
were left blank.
</div>
<br/>
<?endif;?>
It works fine, BUT the problem is that it display the error message like this:
http://www.freemanholland.com/cow/public_html/validation.jpg
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'];
elseif(isset($_SESSION['billingSurname'])):
echo $_SESSION['billingSurname'];
endif; ?>"/>
And now look back at the error message, it gives the input name in the error:
billingSurname
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