PDA

View Full Version : Contact form tutorial validation



nasu
February 21st, 2005, 05:32 PM
First of all contact form tutorial is really nice, but I have a couple of questions. The tutorial assumes that if you've pressed submit button your information is good to go. I would like to validate a couple of fields (for example name and email) if they're not set you would get an error message (i.e you forget to enter a valid e-mail) and link back to the previous page. So any information isn't sent before certain fields are filled correctly.

I would like to use PHP for this because not everyone has their Javascript turned on an it's more secure to check your information serverside, not user agent side.

Any help would be really appriciated,

Thanks in advance!

binime
February 22nd, 2005, 01:44 AM
First of all contact form tutorial is really nice, but I have a couple of questions. The tutorial assumes that if you've pressed submit button your information is good to go. I would like to validate a couple of fields (for example name and email) if they're not set you would get an error message (i.e you forget to enter a valid e-mail) and link back to the previous page. So any information isn't sent before certain fields are filled correctly.

I would like to use PHP for this because not everyone has their Javascript turned on an it's more secure to check your information serverside, not user agent side.

Any help would be really appriciated,

Thanks in advance!


<?php
if(empty(
%4$s POST['field'])) {
echo "Not filled in a field, you will now be transferred back";
header("Location: back.php");
}
if(empty(
%4$s POST['field2'])) {
echo "Not filled in a field, you will now be transferred back";
header("Location: back.php");
}
if(!ValidateEmail($email)) {
echo "email is not valid\n";
header("Location: back.php");
}
else {
do stuff cos they passed
}
function ValidateEmail($email) {
if (eregi("^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,4}$", $email))
{
return true;
}
else {
return false;
}
}
?>


hope this helps

binime
February 22nd, 2005, 01:47 AM
First of all contact form tutorial is really nice, but I have a couple of questions. The tutorial assumes that if you've pressed submit button your information is good to go. I would like to validate a couple of fields (for example name and email) if they're not set you would get an error message (i.e you forget to enter a valid e-mail) and link back to the previous page. So any information isn't sent before certain fields are filled correctly.

I would like to use PHP for this because not everyone has their Javascript turned on an it's more secure to check your information serverside, not user agent side.

Any help would be really appriciated,

Thanks in advance!


<?php
if(empty($_POST['field'])) {
echo "Not filled in a field, you will now be transferred back";
header("Location: back.php");
}
if(empty($_POST['field2'])) {
echo "Not filled in a field, you will now be transferred back";
header("Location: back.php");
}
if(!ValidateEmail($email)) {
echo "email is not valid\n";
header("Location: back.php");
}
else {
do stuff cos they passed
}
function ValidateEmail($email) {
if (eregi("^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,4}$", $email))
{
return true;
}
else {
return false;
}
}
?>


hope this helps