help with validation and error checking

HI,
I am a newbie when come to PHP scripting so I don’t know a lot learning heap by other means but the one thing that I really getting confused with is validation and error checking. I was wondering if someone can help me out ok show me what I need to be doing. for example if same the name , email , age , town, state and postcode (zip-code), is required in the form I like to know how I can check if the users has added a value to the input and to checked if the have typed in there email address.

there is a fair amount of code as it is getting close to the final draft.
I hope that somebody can help me out
thank you Dean
[php]

<?php $myemail = 'root@localhost'; $to = $myemail; $subject = "Your Lady of interest name: $firstName $lastNamt"; // my new dates contact details $firstName = $_POST['name']; //required $lastName = $_POST['last']; $myCity = $_POST['city']; $myPostcode = $_POST['postcode']; $mySuberb = $_POST['suberb']; // required $myState = $_POST['state']; $email_address = $_POST['email']; // required // my new dates personal details $myAge = $_POST['age']; $myHeight = $_POST['height']; $myWeight = $_POST['weight']; // required $myBuild = $_POST['build']; $myEthnicity = $_POST['ethnicity']; $myMaritalstatus = $_POST['maritalstatus']; $Mykids = $_POST['kid']; $myNumberKids =$_POST['kidNum']; $MyKidsAges = $_POST['ages']; //Seeting her My personal Description $aboutMe =$_POST['aboutMe']; $looking =$_POST['looking']; $sexyPart =$_POST['sexyPart']; $why =$_POST['why']; $disL =$_POST['disL']; $sexFirst =$_POST['sexFirst']; $PubicHair =$_POST['hair']; // take the Checkbox Values and printing theminto email message foreach($_POST['myActitive'] as $value) { $myActitivee .= "$value , "; } foreach($_POST['myQuite'] as $value) { $myQuites .= "$value , "; } foreach($_POST['myFood'] as $value) { $myFoods .= "$value , "; } foreach($_POST['myNaughty'] as $value) { $myNaughtys .= "$value , "; } //--------------------------------------------------Main Message------------------------------------------////// $message = "You have received a new message from a lovely lady. Her name is $firstName.\r __________________________________ This is $firstName's bacis Details ---------------------------------- Name: $firstName $lastName $myCity , $mySuberb $myState , $myPostcode $email_address _________________________________ This is $firstName's full Details --------------------------------- My Age: $myAge My height: $myHeight My wieght: $myWeight kg My build is: $myBuild My Ethic Background: $myEthnicity My marital status: $myMaritalstatus $Mykids , $myNumberKids ages are $MyKidsAges ______________________________________________ The activities that $firstName love to do are ---------------------------------------------- $myActitivee __________________________________________ The hobbies that $firstName love to do are ------------------------------------------ $myQuites ________________________________________ The foods that $firstName love to do are ---------------------------------------- $myFoods _________________________________________________ The naughty things that $firstName love to do are ------------------------------------------------- $myNaughtys _____________________________ Tell somthing about your self ----------------------------- $aboutMe ___________________________________________________________________ Tell me what you are looking for and what you like in your partener ------------------------------------------------------------------- $looking _____________________________________ Here is a little bit more about me!! ------------------------------------- My sexiest part of me would be the, $sexyPart why would my $sexyPart be sexy, $why When our date is finished for the night I just like to: $disL When we are saying our good byes for the night I would like to $sexFirst something a little bit private for you my. My pubic hair is, $PubicHair Hope to hear from you soon $firstName $lastName -------------------- "; //--------------------------------------------------Attachment------------------------------------------////// $att = $_FILES['att']; $att_path = $_FILES['att']['tmp_name']; $att_name = $_FILES['att']['name']; $att_size = $_FILES['att']['size']; $att_type = $_FILES['att']['type']; #open, read then close the file $fp = fopen( $att_path, "rb"); $file = fread( $fp, $att_size ); fclose( $fp ); #create a boundary string $num = md5(time()); $str = "==Multipart_Boundary_x{$num}x"; #encode the data for safe transit #and intersperse 76-character chunks with \r\n $file = chunk_split(base64_encode($file)); #define header $hdr = "www.deanross.com.au\n"; $hdr .= "MIME-Version: 1.0\n"; $hdr .= "Content-Type: multipart/mixed;\n "; $hdr .= "boundary=\"{$str}\""; #define message $msg = "This is a multi-part message in MIME format\n\n"; $msg .= "--{$str}\n"; $msg .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n"; $msg .= "Content-Transfer-Encoding: 16bit\n\n"; $msg .= "$message\n\n"; $msg .= "--{$str}\n"; #define the non-text attachment $msg .= "Content-Type: {$att_type};\n"; $msg .= "name=\"{$att_name}\"\n"; $msg .= "Content-Disposition: attachment;\n"; $msg .= "filename=\"{$att_name}\"\n"; $msg .= "Content-Transfer-Encoding: base64\n\n"; $msg .= "$file\n\n"; $msg .= "--{$str}"; #send the email now... $ok = mail( $to, $subject, $msg, $hdr); if($ok) header('Location: ../html/ThankyouPage.html'); ?>

[/php]

Validation is actually quite easy, all you need to do is check to see if its empty or not. There’s more complicated ways of doing it, that’s the easiest.

Take the first one as an example:
$firstName = $_POST[‘name’]; //required

[php]
$error = array();
if(!empty($_POST[‘name’])) {
$firstName = $_POST[‘name’]; //required
} else {
$error[‘name’] = “This field is required”;
}[/php]
Once those are all in place, before you send out the email and do your db insert, you run a check for $error
[php]if(!$error) {
// send email / do insert
}[/php]
On the form, you just do a simple <?=(isset($error['name']) ? $error['name'] : '') ?> where ever you want the message displayed. That’s a shorthand if else statement, easier than having to type out a long if else statement.

If you really want get fancy, I’d suggest reading up on jquery. There’s a library for doing form validation, but its just as easy to code it yourself. I’m learning it now, quite frustrating at times, but its fun when it works :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service