Form Validation - Logic of Required Fields
Posted: Mon Aug 29, 2005 11:26 pm
Hey folks,
For the past 2 1/2 weeks Ive been sincerely struggling with this and can't seem to work it out. Ive got everything else taken care of except for this one topic. I have a total of 10 fields with four fields I wish to have validated. The different sorts of validation I can enter at a later date for the last three of them. All I ask is that you please help me with or hint to me the validation necessary for one field... and Ill do the rest. The name or $visitor field, I figured if I can get human help for one, I shouldn't have a problem getting the others. The code is ALL on one page. It's processing everything correctly and exactly as I want it to. It is only not validating. I know it is a matter of logic, primarily in the if ($_POST['validate'] == 1) { block but I can't figure it out. I have about 20 other .php files that I have hacked to pieces with this one here the one that makes the most sense and actually works.
A good portion is html of course and here is the beginning of the form and the first field:
I have bothered the IRC chaps beyond reproach and have got some nice fellows that have tried to help me but I fear I am helpless concerning this one topic. The validation. I can make it process, upload, make server requests and a bunch of other beginner php stuff without a hitch. I have run through more than a dozen tutorials and a dozen books and conducted countless searches over the past 2 weeks easily and simply cannot grasp this one concept. No book or tut has helped me solve this yet... I can't put the two and two together. I even worked up a story board in a text document:
I can write a one field validation form, no problem. But putting the four together is something Im desperate to learn. If not ultimate help, I would sincerely appreciate a hint or an instruction in the right direction.
Thank you
For the past 2 1/2 weeks Ive been sincerely struggling with this and can't seem to work it out. Ive got everything else taken care of except for this one topic. I have a total of 10 fields with four fields I wish to have validated. The different sorts of validation I can enter at a later date for the last three of them. All I ask is that you please help me with or hint to me the validation necessary for one field... and Ill do the rest. The name or $visitor field, I figured if I can get human help for one, I shouldn't have a problem getting the others. The code is ALL on one page. It's processing everything correctly and exactly as I want it to. It is only not validating. I know it is a matter of logic, primarily in the if ($_POST['validate'] == 1) { block but I can't figure it out. I have about 20 other .php files that I have hacked to pieces with this one here the one that makes the most sense and actually works.
Code: Select all
<?php
$visitor = "";
$business = "";
$state = "";
$country = "";
$visitor_email = "";
$confirm_email = "";
$phone = "";
$fax = "";
$message = "";
$file = "";
if ($_POST['validate'] == 1) {
$visitor = $_POST['visitor'];
$name_error = "Please enter your name";
$business = $_POST['business'];
$state = $_POST['state'];
$country = $_POST['country'];
$visitor_email = $_POST['visitor_email'];
$confirm_email = $_POST['confirm_email'];
$phone = $_POST['phone'];
$fax = $_POST['fax'];
$message = $_POST['message'];
$message_error = "Please enter your message";
$file = $_POST['file'];
$uploadDir = 'upload';
$uploadFile = $uploadDir . $_FILES['file']['name'];
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadFile))
{ //return TRUE;//header ( "Location: /contact/contact_form_sent.htm" );
//else
//{return FALSE; //header ( "Location: /contact/contact_form_sent.htm" );
$recipient = "myemail";
$subject = "Portfolio Website Submission Form";
$message1 .= "E-mail: $visitor_email\nConfirm: $confirm_email\nSubject: $message_subject\n\nName: $visitor\nBusiness: $business\nState: $state\nCountry: $country\nEmail Preferred\nPhone: $phone\nFax: $fax\n\nComments: $comments\n\nIP: $ip\nAgent: $httpagent\nRef: $httpref\n\nFile: $file";
$message2 .= "E-mail: $visitor_email\nConfirm: $confirm_email\nSubject: $message_subject\n\nName: $visitor\nBusiness: $business\nState: $state\nCountry: $country\nPhone Call Preferred\nPhone: $phone\nFax: $fax\n\nComments: $comments\n\nIP: $ip\nAgent: $httpagent\nRef:$httpref\n\nFile: $file";
$headers = "From: Portfolio\n";
$headers .= "Reply-To: $visitor_email";
if (($_POST['validate'] > 0) && (($contact == "Send-Email")) {
mail($recipient,$subject,$message1,$headers);
header ( "Location: /contact/contact_form_sent.htm" );
} else {
mail($recipient,$subject,$message2,$headers);
header ( "Location: /contact/contact_form_sent.htm" );
}
}
}
?>
<html>........A good portion is html of course and here is the beginning of the form and the first field:
Code: Select all
<form action="<?php echo $_SERVER['PHP_SELF']?>" id="contact_test" method="post"
enctype="multipart/form-data">
<fieldset><legend>Demographic Information</legend>
<div class="required">
<?php if ($visitor != "") {echo '<div class="error">'.$name_error.'</div>';} ?>
<label for="visitor">Name:</label>
<input type="text" name="visitor" id="visitor" class="basictext" size="5" maxlength="50"
value="<? echo $name_error; ?>" />
<input type="hidden" name="validate" value="1">
</div>
......I have bothered the IRC chaps beyond reproach and have got some nice fellows that have tried to help me but I fear I am helpless concerning this one topic. The validation. I can make it process, upload, make server requests and a bunch of other beginner php stuff without a hitch. I have run through more than a dozen tutorials and a dozen books and conducted countless searches over the past 2 weeks easily and simply cannot grasp this one concept. No book or tut has helped me solve this yet... I can't put the two and two together. I even worked up a story board in a text document:
Which did no good either.I) Blank form√
Within PHP, fields are identified as blank $visitor=””; √
a) User enters input onto form
1) Form is submitted
Form must carry the values via $_POST
2) Form is validated
Tested – Email Syntax, Empty fields, Numbers only fields, Letters only fields
II) If validation is successful, form is sent to my email address with info
a) Redirected to congrats page
Header()
III) If validation is a failure, form is redisplayed
$_SERVER(“PHP_SELF”);
a) Previous successful and validated user input is retained and displayed
$_POST
b) Failed input is highlighted
CSS Layout with color, PHP test
IV) User enters correct fields
a) User resubmits the form
repeated
1) Form is checked again for validation
repeated
V) If success, form is submitted
Repeated
a) Redirected to congrats page
Repeated
VI) If fail, repeat procedure until successful validation
Repeated
I can write a one field validation form, no problem. But putting the four together is something Im desperate to learn. If not ultimate help, I would sincerely appreciate a hint or an instruction in the right direction.
Thank you