PDA

View Full Version : Form Validation?


obaluba
02-13-2007, 07:40 PM
I know this has been asked a hundred times, I've browsed previous threads but cant see an answer which will help me.

I have a simple form, which when filled out enters data into a database and sends a formmail email to me, confirming entry into the db.

What I would like to do is ensure that the user fills in certain fields, like firstname, lastname, address and email.

What would be the easiest way to do this? Is it something I would have to do on the form istelf or can it be done here?

My code is as follows:

The Store Form in database code:

<? // code for store_comment.php

// Database information required to connect to database
$host = "localhost";
$name = "cd";
$pass = "1234";
$dbname = "cd_join";
$message = "";

// Connect to Database and select the database to use
$dbi = mysql_connect($host,$name,$pass) or
die("I cannot connect to the database. Error :" . mysql_error());
mysql_select_db($dbname,$dbi);



// Get the values posted from the Form in Join.php
$title = $_POST["title"];
$firstname = $_POST["firstname"];
$lastname = $_POST["lastname"];
$dateofbirth = $_POST["dateofbirth"];
$address = $_POST["address"];
$county = $_POST["county"];
$postcode = $_POST["postcode"];
$mobile_no = $_POST["mobile_no"];
$home_no = $_POST["home_no"];
$email_address = $_POST["email_address"];
$forumusername = $_POST["forumusername"];
$hearaboutaoc = $_POST["hearaboutaoc"];
$model = $_POST["model"];
$vehicle_year = $_POST["vehicle_year"];
$enginesize = $_POST["enginesize"];
$colour = $_POST["colour"];
$registration = $_POST["registration"];
$membershiptype = $_POST["membershiptype"];
$paymenttype = $_POST["paymenttype"];

if ($firstname != "" ) {
$message = "Please enter a First Name.";
}

// Insert the Details into the Database
$sql = "INSERT INTO join (title,firstname,lastname,dateofbirth,address,county,postcode,mobile_no,home_no,email_address,forumu sername,hearaboutaoc,model,vehicle_year,enginesize,colour,registration,membershiptype,paymenttype) VALUES
('$title','$firstname','$lastname','$dateofbirth','$address','$county','$postcode','$mobile_no','$ho me_no','$email_address','$forumusername','$hearaboutaoc','$model','$vehicle_year','$enginesize','$co lour','$registration','$membershiptype','$paymenttype')";
$result = mysql_query($sql,$dbi);

// Formmail Section
$mailto = 'rrr@rr.com' ;
$subject = "Membership Request" ;
$formurl = "http://www.cd1.co.uk/index.php" ;
$errorurl = "http://www.cd1.co.uk/index.php" ;
$thankyouurl = "http://www.cd1.co.uk/join_payment.php" ;

$title = $_POST["title"];
$firstname = $_POST["firstname"];
$lastname = $_POST["lastname"];
$dateofbirth = $_POST["dateofbirth"];
$address = $_POST["address"];
$county = $_POST["county"];
$postcode = $_POST["postcode"];
$mobile_no = $_POST["mobile_no"];
$home_no = $_POST["home_no"];
$email_address = $_POST["email_address"];
$forumusername = $_POST["forumusername"];
$hearabout = $_POST["hearabout"];
$model = $_POST["model"];
$vehicle_year = $_POST["vehicle_year"];
$enginesize = $_POST["enginesize"];
$colour = $_POST["colour"];
$registration = $_POST["registration"];
$membershiptype = $_POST["membershiptype"];
$paymenttype = $_POST["paymenttype"];
$http_referrer = getenv( "HTTP_REFERER" );

$messageproper =

"------------------------- Membership Request Submission Form -------------------------\n\n" .
"Title: $title" .
"\n\n" .
"Firstname: $firstname" .
"\n\n" .
"Lastname: $lastname" .
"\n\n" .
"Date Of Birth: $dateofbirth" .
"\n\n" .
"Address: $address" .
"\n\n" .
"County: $county" .
"\n\n" .
"Postcode: $postcode" .
"\n\n" .
"Mobile Number: $mobile_no" .
"\n\n" .
"Home Number: $home_no" .
"\n\n" .
"Email Address: $email_address" .
"\n\n" .
"Forum Username: $forumusername" .
"\n\n" .
"How Did you Hear about AOC: $hearaboutaoc" .
"\n\n" .
"Vehicle Model: $model" .
"\n\n" .
"Vehicle Year: $vehicle_year" .
"\n\n" .
"Engine Size: $enginesize" .
"\n\n" .
"Colour: $colour" .
"\n\n" .
"Registration: $registration" .
"\n\n" .
"Membership Type: $membershiptype" .
"\n\n" .
"Payment Type: $paymenttype" .
"\n\n" .

"\n\n------------------------------------------------------------\n" ;



mail($mailto, $subject, $messageproper, "From: \"$firstname $lastname\" <$email_address>\nReply-To: \"$firstname\" <$email_address>\nX-Mailer: chfeedback.php 2.01" );
header( "Location: $thankyouurl" );
exit ;

?>



thanks for looking :)

rafiki
02-13-2007, 07:58 PM
ys can be done on that page
if ($firstname != "" ) {
$message = "Please enter a First Name.";
}
this checks that the value of $firstname isnt empty the sets the var $message to Please enter a First Name.
when really you need to echo it out
also, i would use a if statement to check all of them, if one has an empty field dont add stuff into the database, or email your self the results :)

rafiki
02-13-2007, 08:00 PM
also add an ' to the start and end of the $messageproper var, like so


$messageproper =

' "------------------------- Membership Request Submission Form -------------------------\n\n" .
"Title: $title" .
"\n\n" .
"Firstname: $firstname" .
"\n\n" .
"Lastname: $lastname" .
"\n\n" .
"Date Of Birth: $dateofbirth" .
"\n\n" .
"Address: $address" .
"\n\n" .
"County: $county" .
"\n\n" .
"Postcode: $postcode" .
"\n\n" .
"Mobile Number: $mobile_no" .
"\n\n" .
"Home Number: $home_no" .
"\n\n" .
"Email Address: $email_address" .
"\n\n" .
"Forum Username: $forumusername" .
"\n\n" .
"How Did you Hear about AOC: $hearaboutaoc" .
"\n\n" .
"Vehicle Model: $model" .
"\n\n" .
"Vehicle Year: $vehicle_year" .
"\n\n" .
"Engine Size: $enginesize" .
"\n\n" .
"Colour: $colour" .
"\n\n" .
"Registration: $registration" .
"\n\n" .
"Membership Type: $membershiptype" .
"\n\n" .
"Payment Type: $paymenttype" .
"\n\n" .

"\n\n------------------------------------------------------------\n" ' ;

neomaximus2k
02-13-2007, 08:38 PM
you could use JS in conjunction with the code above

obaluba
02-14-2007, 12:03 AM
What would an If statement look like to do that procedure? Is there not one I can use then reapply to each field which requires validation?

thanks for the replies so far :)

Nightfire
02-14-2007, 01:05 AM
Do that and none of the variables or newlines will work.

also add an ' to the start and end of the $messageproper var, like so


$messageproper =

' "------------------------- Membership Request Submission Form -------------------------\n\n" .
"Title: $title" .
"\n\n" .
"Firstname: $firstname" .
"\n\n" .
"Lastname: $lastname" .
"\n\n" .
"Date Of Birth: $dateofbirth" .
"\n\n" .
"Address: $address" .
"\n\n" .
"County: $county" .
"\n\n" .
"Postcode: $postcode" .
"\n\n" .
"Mobile Number: $mobile_no" .
"\n\n" .
"Home Number: $home_no" .
"\n\n" .
"Email Address: $email_address" .
"\n\n" .
"Forum Username: $forumusername" .
"\n\n" .
"How Did you Hear about AOC: $hearaboutaoc" .
"\n\n" .
"Vehicle Model: $model" .
"\n\n" .
"Vehicle Year: $vehicle_year" .
"\n\n" .
"Engine Size: $enginesize" .
"\n\n" .
"Colour: $colour" .
"\n\n" .
"Registration: $registration" .
"\n\n" .
"Membership Type: $membershiptype" .
"\n\n" .
"Payment Type: $paymenttype" .
"\n\n" .

"\n\n------------------------------------------------------------\n" ' ;

Nightfire
02-14-2007, 01:09 AM
Do that and none of the variables or newlines will work.

also add an ' to the start and end of the $messageproper var, like so


$messageproper =

' "------------------------- Membership Request Submission Form -------------------------\n\n" .
"Title: $title" .
"\n\n" .
"Firstname: $firstname" .
"\n\n" .
"Lastname: $lastname" .
"\n\n" .
"Date Of Birth: $dateofbirth" .
"\n\n" .
"Address: $address" .
"\n\n" .
"County: $county" .
"\n\n" .
"Postcode: $postcode" .
"\n\n" .
"Mobile Number: $mobile_no" .
"\n\n" .
"Home Number: $home_no" .
"\n\n" .
"Email Address: $email_address" .
"\n\n" .
"Forum Username: $forumusername" .
"\n\n" .
"How Did you Hear about AOC: $hearaboutaoc" .
"\n\n" .
"Vehicle Model: $model" .
"\n\n" .
"Vehicle Year: $vehicle_year" .
"\n\n" .
"Engine Size: $enginesize" .
"\n\n" .
"Colour: $colour" .
"\n\n" .
"Registration: $registration" .
"\n\n" .
"Membership Type: $membershiptype" .
"\n\n" .
"Payment Type: $paymenttype" .
"\n\n" .

"\n\n------------------------------------------------------------\n" ' ;


Just off the top of my head, to check all fields are filled

$error = '';
foreach($_POST as $check){
if($check == ''){
$error .= 'Please make sure '.$check.' is filled<br>';
}
echo $error;

rafiki
02-14-2007, 01:11 PM
Do that and none of the variables or newlines will work.



Just off the top of my head, to check all fields are filled

$error = '';
foreach($_POST as $check){
if($check == ''){
$error .= 'Please make sure '.$check.' is filled<br>';
}
echo $error;


nice didn't no about the foreach to check all fields, also what do you mean by do that and none of the new lines will work? does for me

Nightfire
02-14-2007, 01:15 PM
Because you put everything inside single quotes ' '. Variables, newlines, tabs, etc will not work under single quotes, but under double quotes " ". try it out


<?php
$string = "hello there\n\t<br>"
echo '$string';
echo "$string";
echo $string;
?>

Take a look at the source for the new lines and tabs

rafiki
02-14-2007, 01:20 PM
Ok gotchya i thought i was doing ""'s my keybaord is running out of batterys and is set to US instead of EN so i get some unknown results from it lol,
Thanks for explaining

Parse error: parse error, unexpected T_ECHO in C:\Program Files\xampp\xampp\htdocs\Jay\boo.php on line 3

in fact it doesnt echo any of them?
does it matter about the new lines on a windows system? "\n\t"?

obaluba
02-14-2007, 08:07 PM
sorted, thanks guys :)

Nightfire
02-14-2007, 08:09 PM
Ok gotchya i thought i was doing ""'s my keybaord is running out of batterys and is set to US instead of EN so i get some unknown results from it lol,
Thanks for explaining

Parse error: parse error, unexpected T_ECHO in C:\Program Files\xampp\xampp\htdocs\Jay\boo.php on line 3

in fact it doesnt echo any of them?
does it matter about the new lines on a windows system? "\n\t"?

I forgot to add a semi-colon on the end of the first line :)