Hi Gurus,
I have a form that works well. But there is no validation. Can validation be added on after the fact? I'm sure it's not ideal but can it be done. General suggestion appreciated on how.

Here's the code

<?php

if(isset($_POST["Submit"]))
{
require("c:\php\includes\class.phpmailer.php");

$mail = new PHPMailer();

$toaddress = "rocky@airedale911.org";  //Change this to the email address you will be receiving your notices.
$mailhost = "mail.airedale911.org";  //Change this to your actual Domain name.
$fromaddress = "rocky@airedale911.org";  //Change this to the email address you will use to send and authenticate with.
$frompwd = "notarealpassword";  //Change this to the above email addresses password.
$subject = "Airedale 911 Application Form";  //Change this to your own email message subject.


$referredby = $_POST["Referredby"];
$fromname = $_POST["Name"];
$street = $_POST["Street"] ;
$city = $_POST["City"];
$state = $_POST["State"];
$zip = $_POST["Zip"];
$homephone = $_POST["Homephone"] ;
$workphone = $_POST["Workphone"];
$cellphone = $_POST["Cellphone"];
$rplyto = $_POST["Email"];
$family = $_POST["Family"] ;
$sex = $_POST["Sex"] ;
$previouslyowned = $_POST["Previouslyowned"] ;
$whenpreviouslyowned = $_POST["Whenpreviouslyowned"] ;
$sexpreviouslyowned = $_POST["Sexpreviouslyowned"] ;
$otherbreeds = $_POST["Otherbreeds"] ;
$fateoflastdog = $_POST["Fateoflastdog"] ;
$lostadog = $_POST["Lostadog"] ;
$movegiveupdog = $_POST["Movegiveupdog"] ;
$otherpets = $_POST["Otherpets"] ;
$otherpetsages = $_POST["Otherpetsages"] ;
$otherpetsspayedneutered = $_POST["Otherpetsspayedneutered"] ;
$otherpetscurrentlylicensed = $_POST["Otherpetscurrentlylicensed"] ;
$fencedinyard = $_POST["Fencedinyard"] ;
$kindoffence = $_POST["Kindoffence"] ;
$howhighfence = $_POST["Howhighfence"] ;
$sizeoffencedinarea = $_POST["Sizeoffencedinarea"] ;
$runofhouse = $_POST["Runofhouse"] ;
$allowedonfurniture = $_POST["Allowedonfurniture"] ;
$havebedandblanket = $_POST["Havebedandblanket"] ;
$dogbedinwhatroom = $_POST["Dogbedinwhatroom"] ;
$wherewillairedalesleep = $_POST["Wherewillairedalesleep"] ;
$hoursnoonehome = $_POST["Hoursnoonehome"] ;
$daysaweekalone = $_POST["Daysaweekalone"] ;
$whatroomabovetimes = $_POST["Whatroomabovetimes"] ;
$travelingwherewilldogstay = $_POST["Travelingwherewilldogstay"] ;
$familiardogcrate = $_POST["Familiardogcrate"] ;
$whenwheredogcrate = $_POST["Whenwheredogcrate"] ;
$reference1 = $_POST["Reference1"] ;
$reference2 = $_POST["Reference2"] ;
$vetinfo = $_POST["Vetinfo"] ;
$comments = $_POST["Comments"] ;







$msgbody = "Referred by  = " . $referredby
."<br>"
."Your Full Name  = " . $fromname
."<br>"
."Your Street Address  = " . $street
."<br>"
."City  = " . $city
."<br>"
."State  = " . $state
."<br>"
."Zip Code  = " . $zip
."<br>"
."Your home telephone number (xxx-xxx-xxxx)  = " . $homephone
."<br>"
."Your work number  = " . $workphone
."<br>"
."Your cell phone number  = " . $cellphone
."<br>"
."Email  = " . $rplyto
."<br>"
."lease list names and ages of all family members (including yourself), live-in help, frequent visitors, etc.  = " . $family
."<br>"
."Do you prefer a male or female Airedale?  = " . $sex
."<br>"
."Have you previously owned an Airedale?  = " . $previouslyowned
."<br>"
."When?  = " . $whenpreviouslyowned
."<br>"
."Male or Female?  = " . $sexpreviouslyowned
."<br>"
."What other dog breeds have you owned?  = " . $otherbreeds
."<br>"
."What happened to your last dog?  = " . $fateoflastdog
."<br>"
."Have you ever lost a dog other than through death? = " . $lostadog
."<br>"
."If you had to move, would you consider giving up your dog?  = " . $movegiveupdog
."<br>"
."What other pets do you currently have?  = " . $otherpets
."<br>"
."Their ages  = " . $otherpetsages
."<br>"
."Are they spayed/neutered?  = " . $otherpetsspayedneutered
."<br>"
."Are they currently licensed?  = " . $otherpetscurrentlylicensed
."<br>"
."Do you a fenced-in yard?  = " . $fencedinyard
."<br>"
."What kind of fence?  = " . $kindoffence
."<br>"
."How high is the fence?  = " . $howhighfence
."<br>"
."Size of fenced-in area?  = " . $sizeoffencedinarea
."<br>"
."Will your new Airedale have run of the house?  = " . $runofhouse
."<br>"
."Be allowed on the furniture?  = " . $allowedonfurniture
."<br>"
."Have his/her own dog bed and blankets?  = " . $havebedandblanket
."<br>"
."In what room will the dog bed be placed?  = " . $dogbedinwhatroom
."<br>"
."In what room will your new Airedale sleep?  = " . $wherewillairedalesleep
."<br>"
."How many hours at a time is no one at home?  = " . $hoursnoonehome
."<br>"
."How many days a week will your dog be left alone?  = " . $daysaweekalone
."<br>"
."In what room(s) will your dog be during the above times?  = " . $whatroomabovetimes
."<br>"
."When you travel, where will your dog stay?  = " . $travelingwherewilldogstay
."<br>"
."Are you familiar with a dog crate?  = " . $familiardogcrate
."<br>"
."When and where do you use one?  = " . $whenwheredogcrate
."<br>"
."Name, address, phone number  = " . $reference1
."<br>"
."Name, address, phone number  = " . $reference2
."<br>"
."Your veterinarian's name, address, phone number  = " . $vetinfo
."<br>"
."Comments  = " . $comments; 



$mail->IsSMTP();
$mail->Host = $mailhost;
$mail->SMTPAuth = true;
$mail->Username = $fromaddress;
$mail->Password = $frompwd;

$mail->From = $fromaddress;
$mail->FromName = $fromname;
$mail->AddReplyTo($rplyto); 
$mail->AddAddress($toaddress); 
$mail->IsHTML(true);
$mail->Subject = $subject;
$mail->Body = $msgbody;

if(!$mail->Send())



{
   echo "Message could not be sent. <p>";
   echo "Mailer Error: " . $mail->ErrorInfo;
   exit;
}
 
echo "Thank you, your message has been sent!";
}

?>

<html>

<head>

<style>
.mystyle {font-size:15px; font-weight:600; font-family:Arial, Helvetica, sans-serif; font-variant:normal; letter-spacing:normal; font-size-adjust:inherit}
TD.mystyle {font-size:15px; font-weight:600; font-family:Arial, Helvetica, sans-serif; font-variant:normal letter-spacing:normal; font-size-adjust:inherit}
TR TD.mystyle {font-size:15px; font-weight:600; font-family:Arial, Helvetica, sans-serif; font-variant:normal letter-spacing:normal; font-size-adjust:inherit}
TABLE TR TD.mystyle {font-size:15px; font-weight:600; font-family:Arial, Helvetica, sans-serif; font-variant:normal letter-spacing:normal; font-size-adjust:inherit}
DIV TABLE TR TD.mystyle {font-size:15px; font-weight:600; font-family:Arial, Helvetica, sans-serif; font-variant:normal letter-spacing:normal; font-size-adjust:inherit}
</style>

</head>


<body>

<DIV class="mystyle">

<form name="SendEmail01" method="post">

<table border=0>
<tr>
	<td class="mystyle">Referred by:</td>
	<td><input type="text" name="Referredby" size="30"></td>
</tr>
<tr>
	<td class="mystyle">Your Full Name:</td>
	<td><input type="text" name="Name" size="30"></td>
</tr>
<tr>
	<td class="mystyle">Your Street Address:</td>
	<td><input type="text" name="Street" size="30"></td>
</tr>
<tr>
	<td class="mystyle">City:</td>
	<td><input type="text" name="City" size="30"></td>
</tr>
<tr>
	<td class="mystyle">State:</td>
	<td><input type="text" name="State" size="30"></td>
</tr>
<tr>
	<td class="mystyle">Zip Code:</td>
	<td><input type="text" name="Zip" size="30"></td>
</tr>
<tr>
	<td class="mystyle">Your home telephone number (xxx-xxx-xxxx) :</td>
	<td><input type="text" name="Homephone" size="30"></td>
</tr>
<tr>
	<td class="mystyle">Your work number:</td>
	<td><input type="text" name="Workphone" size="30"></td>
</tr>
<tr>
	<td class="mystyle">Your cell phone number:</td>
	<td><input type="text" name="Cellphone" size="30"></td>
</tr>
<tr>
	<td class="mystyle">Email:</td>
	<td><input type="text" name="Email" size="30"></td>
</tr>
<tr>
	<td class="mystyle">Please list names and ages of all family members (including yourself), live-in help, frequent visitors, etc.:</td>
	<td><textarea rows="4" name="Family" cols="30"></textarea></td>
</tr>
<tr>
	<td class="mystyle">Do you prefer a male or female Airedale?:</td>
	<td><input type="radio" name="Sex" value="Male" checked="checked"> Male <br>
        <input type="radio" name="Sex" value="Female"> Female<br>
        <input type="radio" name="Sex" value="No preference"> No Preference<br>
    </td>
</tr>
<tr>
	<td>        </td>
</tr>
<tr>
	<td>        </td>
</tr>
<tr>
	<td class="mystyle">Have you previously owned an Airedale?:</td>
	<td><input type="radio" name="Previouslyowned" value="Yes"> Yes <br>
        <input type="radio" name="Previouslyowned" value="No" checked="checked"> No <br>
    </td>
</tr>
<tr>
	<td class="mystyle">When?:</td>
	<td><input type="text" name="Whenpreviouslyowned" size="30"></td>
</tr>
<td class="mystyle">Male or Female?:</td>
<td><input type="radio" name="Sexpreviouslyowned" value="Male" checked="checked"> Male <br>
        <input type="radio" name="Sexpreviouslyowned" value="Female"> Female<br>
        <input type="radio" name="Sexpreviouslyowned" value="N/A"> N/A<br>
</td>
<tr>
	<td class="mystyle">What other dog breeds have you owned?:</td>
	<td><input type="text" name="Otherbreeds" size="30"></td>
</tr>
<tr>
	<td class="mystyle">What happened to your last dog?:</td>
	<td><input type="text" name="Fateoflastdog" size="30"></td>
</tr>
<tr>
	<td class="mystyle">Have you ever lost a dog other than through death?:</td>
	<td><input type="text" name="Lostadog" size="30"></td>
</tr>
<tr>
	<td class="mystyle">If you had to move, would you consider giving up your dog?:</td>
	<td><input type="text" name="Movegiveupdog" size="30"></td>
</tr>
<tr>
	<td class="mystyle">What other pets do you currently have?:</td>
	<td><input type="text" name="Otherpets" size="30"></td>
</tr>
<tr>
	<td class="mystyle">Their ages:</td>
	<td><input type="text" name="Otherpetsages" size="30"></td>
</tr>
<tr>
	<td class="mystyle">Are they spayed/neutered?:</td>
	<td><input type="text" name="Otherpetsspayedneutered" size="30"></td>
</tr>
<tr>
	<td class="mystyle">Are they currently licensed?:</td>
	<td><input type="text" name="Otherpetscurrentlylicensed" size="30"></td>
</tr>
<tr>
	<td class="mystyle">Do you a fenced-in yard?:</td>
	<td><input type="text" name="Fencedinyard" size="30"></td>
</tr>
<tr>
	<td class="mystyle">What kind of fence?:</td>
	<td><input type="text" name="Kindoffence" size="30"></td>
</tr>
<tr>
	<td class="mystyle">How high is the fence?:</td>
	<td><input type="text" name="Howhighfence" size="30"></td>
</tr>
<tr>
	<td class="mystyle">Size of fenced-in area?:</td>
	<td><input type="text" name="Sizeoffencedinarea" size="30"></td>
</tr>
<tr>
	<td class="mystyle">Will your new Airedale have "run of the house"?:</td>
	<td><input type="radio" name="Runofhouse" value="Yes"> Yes <br>
        <input type="radio" name="Runofhouse" value="No" checked="checked"> No <br>
    </td>
</tr>
<tr>
	<td>        </td>
</tr>
<tr>
	<td>        </td>
</tr>
<tr>
	<td class="mystyle">Be allowed on the furniture?:</td>
	<td><input type="radio" name="Allowedonfurniture" value="Yes"> Yes <br>
        <input type="radio" name="Allowedonfurniture" value="No" checked="checked"> No <br>
    </td>
</tr>
<tr>
	<td>        </td>
</tr>
<tr>
	<td>        </td>
</tr>
<tr>
	<td class="mystyle">Have his/her own dog bed and blankets?:</td>
	<td><input type="radio" name="Havebedandblanket" value="Yes"> Yes <br>
        <input type="radio" name="Havebedandblanket" value="No" checked="checked"> No <br>
    </td>
</tr>
<tr>
	<td class="mystyle">In what room will the dog bed be placed?:</td>
	<td><input type="text" name="Dogbedinwhatroom" size="30"></td>
</tr>
<tr>
	<td class="mystyle">In what room will your new Airedale sleep?:</td>
	<td><input type="text" name="Wherewillairedalesleep" size="30"></td>
</tr>
<tr>
	<td class="mystyle">How many hours at a time is no one at home?:</td>
	<td><input type="text" name="Hoursnoonehome" size="30"></td>
</tr>
<tr>
	<td class="mystyle">How many days a week will your dog be left alone?:</td>
	<td><input type="text" name="Daysaweekalone" size="30"></td>
</tr>
<tr>
	<td class="mystyle">In what room(s) will your dog be during the above times?:</td>
	<td><input type="text" name="Whatroomabovetimes" size="30"></td>
</tr>
<tr>
	<td class="mystyle">When you travel, where will your dog stay?:</td>
	<td><input type="text" name="Travelingwherewilldogstay" size="30"></td>
</tr>
<tr>
	<td class="mystyle">Are you familiar with a dog crate?:</td>
	<td><input type="radio" name="Familiardogcrate" value="Yes"> Yes <br>
        <input type="radio" name="Familiardogcrate" value="No" checked="checked"> No <br>
    </td>
</tr>
<tr>
	<td class="mystyle">When and where do you use one?:</td>
	<td><textarea rows="4" name="Whenwheredogcrate" cols="30"></textarea></td>
</tr>
<br><br>
<tr>
	<td class="mystyle">REFERENCES: Name, address, phone number:</td>
	<td><textarea rows="4" name="Reference1" cols="30"></textarea></td>
</tr>
<tr>
	<td class="mystyle">REFERENCES: Name, address, phone number:</td>
	<td><textarea rows="4" name="Reference2" cols="30"></textarea></td>
</tr>
<tr>
	<td class="mystyle">REFERENCES: Your veterinarian's name, address, phone number:</td>
	<td><textarea rows="4" name="Vetinfo" cols="30"></textarea></td>
</tr>
<tr>
	<td class="mystyle">Comments:</td>
	<td><textarea rows="8" name="Comments" cols="30"></textarea></td>
</tr>
<tr>
	<td><input type="submit" name="Submit" value="Submit"></td>
</tr>

</table>
</form>
</div>

</body></html>

Thanks much
SqUarkMAn

Yes, you usually do add validation later.

Simply you would want to make sure all form fields were filled in.

To check you would do:

if (isset($_POST['field']) && $_POST['field']) {
// it was filled in
}

If it wasn't you could give a message, stating that a particular field or set of fields are missing.

Then in the form fields, you'll want to 'remember' the user submitted values.

eg:

<input type="text" name="Referredby" size="30" value="<?php echo $_POST['Referredby']; ?>" />

oops...

instead of doing like yours $var = $_REQUEST

i will do like the following

foreach($_REQUEST as $key => $v)
$$key = $v;

the above code will convert all form variables into php variables
you could save lot of lines and typing work...

and here is one solution to your problem

foreach($_REQUEST as $key => $value)
if(empty($key)) echo $key." is empty";

and additionally what i might do is

i will create an associative array for the empty keys description
so like if the $key is firstname and if it is empty then displaying
"firstname is empty" wont be appropriate instead we will like to do
like "First Name is empty"

so create an associative array which will hold the actual texts for the
variables ($key) and display it...

and instead of doing like the following
"once ther lived ". $value

i would prefer like "once there lived $value";

how about you?

Hi...
I think its better to use javascript for the validation..
There are mainly 2 reasons for this purpose...

1) Using javascript it looks more professional.. You tell the user on the client side itself without submitting the form..
2) Once the form is submitted, then to validate, you also need to store all the values once.. maybe in a session.. so that we can return back to the form... Because the user will not like to fill in the whole form once again.. specially when its as big as ur form...

So its better to use javascript validation...

<script type="text/javascript" language="javascript">

function validate(frmobj)
{
  name = frmobj.Name.value;
  if(name=="")
  {
      alert("Please input your full name");
      frmobj.Name.value="";
      frmobj.Name.focus();
      return false;
  }

  street = frmobj.Street.value;
  if(name=="")
  {
      alert("Please input your Street");
      frmobj.Street.value="";
      frmobj.Street.focus();
      return false;
  }


// Once the whole testing is done...
// then return true to tell browser.. that form is validated
  
  return true;
}
</script>

Once the javascript is coded..
then you can proceed with calling this function from form...

<form method="post" action="....somevalue...." onSubmit="javascript:return validate(this)">

</form>

You do not need to write the full document.formname.field.value..
since we are passing the this parameter that works as an object for the corresponding field in javascript..

Let me see if I understand.
You are saying JS is better because you won't have to store data to be retrieved since the validation is done at the time the form is filled out on client side. So you eliminate a database storage and you don't have to submit to validate.

Hunnn.. Yes.. You are almost correct...

In case of storage of data, its not in database when some input is wrong..

Suppose when some value is in error... then what you were doing..

1) submit the form
2) check validation in PHP...
3) link back to the form...

But at this point the form is empty,, since you are reloading the form..
so you have to temporary store the information that the user entered so that the user dont have to type the whole information again...
(its always irritating when u have to fill in the form which has more than 10 fields again and again, keep urself in place of a user)..

So you have to provide with some temporary storage when the form is submitted... the best option is sessions... Then again u clear sessions, and so on....
So in short it is a long procedure...

Thats y i said that before you actually write data to database, validate the information from javascript, so that once the form is submitted, there is no error in the data.. It is in the similar format/way you actually want from user... And finally, the form is submitted only once.....

Hunnn.. Yes.. You are almost correct...

In case of storage of data, its not in database when some input is wrong..

Suppose when some value is in error... then what you were doing..

1) submit the form
2) check validation in PHP...
3) link back to the form...

But at this point the form is empty,, since you are reloading the form..
so you have to temporary store the information that the user entered so that the user dont have to type the whole information again...
(its always irritating when u have to fill in the form which has more than 10 fields again and again, keep urself in place of a user)..

So you have to provide with some temporary storage when the form is submitted... the best option is sessions... Then again u clear sessions, and so on....
So in short it is a long procedure...

Thats y i said that before you actually write data to database, validate the information from javascript, so that once the form is submitted, there is no error in the data.. It is in the similar format/way you actually want from user... And finally, the form is submitted only once.....

It is good to have both server and client side validation, as some clients don't have JavaScript support.

JavaScript does increase the usability more then server side validation due to its faster feedback.

It is good to have both server and client side validation, as some clients don't have JavaScript support.

JavaScript does increase the usability more then server side validation due to its faster feedback.

Yes.. I agree o digital-ether.... Thats a more safe idea...
You can use both of them to be tooooo sure that ur data is properly validated....

Otherwise most of the servers have javascript support with them... So i am usually assured that javascript thing will work...

Rest it depends upon developer to developer... you can add both.. absolutely no problem..

Since javascript would already have validated the data, PHP validation would be true as well (condition that you have applied similar validation in both javascript and php).. and php would carry on with the normal form submit thing...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.