Jump to content

Form Validation


stublackett

Recommended Posts

Hi,

 

I've got a form validator made, It works with the errors i.e if a user doesnt fill in a certain field it tells them

 

What I need to know is if the information is filled in correctly how do I take the form further??

 

I've tried

// If everything is filled out print the message.
if($firstname && $surname && $email) {

{ 

 

What I'd like it to do is re-direct to a page using header("Location: success.php");

 

Any help appreciated

 

My code is

<?php #Form Validation & Calculation

// check for submission
if (isset($_POST['submit'])) { 

//Collect Post Values
//Total for Coach Trip
$kids = $_POST['NUMBER-OF-KIDS']; //Post value for kids
$adults = $_POST['NO-OF-ADULTS2']; //Post value for adults
$specialneeds = $_POST['NUMBER-OF-SPECIAL-NEEDS']; //Post value for Special Needs
$carers = $_POST['NUMBER-OF-CARERS']; //Post Value For Carers
$seniors = $_POST['NO-OF-S-CITIZENS']; //Post Value for Senior Citizens
$under3 = $_POST['NO-OF-U3']; //Post Value for Number Of Under 3's

//Validation for Form
$firstname = $_POST['FIRST-NAME']; //Post Value for First Name
$surname = $_POST['SURNAME']; //Post Value for Surname
$email = $_POST['EMAIL']; //Post Value for E-Mail

$total = $kids + $adults + $specialneeds + $carers + $seniors + $under3;

if ( $total <= 14) {
   $errormsg = "** To make a coach booking there must be 15 or more people in your Coach Party<b>";
   $formerror = "<b>Please correct the errors on the form before proceeding</b>";
   } else {
   $errormsg = ""; 
   }
if (!empty($_POST['FIRST-NAME'])) {
$firstname = $_POST['username'];
}else{
$firstname = NULL;
$firstnamerror = '<b>** Please enter a First Name</b>';
$formerror = "<b>Please correct the errors on the form before proceeding</b>";
}

if (!empty($_POST['SURNAME'])) {
$surname = $_POST['SURNAME'];
}else{
$surname = NULL;
$surnamerror = '<b>** Please enter a Surname</b>';
$formerror = "<b>Please correct the errors on the form before proceeding</b>";
}

if (!empty($_POST['EMAIL'])) {
$email = $_POST['EMAIL'];
}else{
$email = NULL;
$emailerror = '<b>** Please enter an Email address</b>';
$formerror = "<b>Please correct the errors on the form before proceeding</b>";
}
    
}

?>

Link to comment
Share on other sites

Well i didn't completely follow your code, you seem to have $errormsg and $formerror? In any case, what you should do is define a variable at the beginning of the script which is then alterered throughout your checking process. At the end, check the value of that variable and then do whatever you need.

 

You could do that with your error message variables -- just define them at the beginning as an empty string, then check at the end to see if they are still empty.

 

Alternatively, a better approach would be to wrap up all the error checking in a function which can then return true or false.

Link to comment
Share on other sites

The $errormsg etc are all echoed in the code, I dont have any real concerns or issues with them, As they are shown in a DIV Tag

 

What I need to do is check to see if the required fields are filled in and if they are then send the form to a new page

 

and the only way to do that (That I know) is via an

if ($variable && $variable ) { etc, etc!

 

Its just then directing to the required page after that.... ?

Link to comment
Share on other sites

I've given it a try

 

What I'd like it to do is if $firstname $surname and $email is filled in, It then sends the E-Mail to the customer

 

Code now looks like this :

 

<?php #Form Validation & Calculation

// check for submission
if (isset($_POST['submit'])) { 

//Collect Post Values
//Total for Coach Trip
$kids = $_POST['NUMBER-OF-KIDS']; //Post value for kids
$adults = $_POST['NO-OF-ADULTS2']; //Post value for adults
$specialneeds = $_POST['NUMBER-OF-SPECIAL-NEEDS']; //Post value for Special Needs
$carers = $_POST['NUMBER-OF-CARERS']; //Post Value For Carers
$seniors = $_POST['NO-OF-S-CITIZENS']; //Post Value for Senior Citizens
$under3 = $_POST['NO-OF-U3']; //Post Value for Number Of Under 3's

//Validation for Form
$firstname = $_POST['FIRST-NAME']; //Post Value for First Name
$surname = $_POST['SURNAME']; //Post Value for Surname
$email = $_POST['EMAIL']; //Post Value for E-Mail

$total = $kids + $adults + $specialneeds + $carers + $seniors + $under3;

if ( $total <= 14) {
   $errormsg = "** To make a coach booking there must be 15 or more people in your Coach Party<b>";
   $formerror = "<b>Please correct the errors on the form before proceeding</b>";
   } else {
   $errormsg = ""; 
   }
if (!empty($_POST['FIRST-NAME'])) {
$firstname = $_POST['username'];
}else{
$firstname = NULL;
$firstnamerror = '<b>** Please enter a First Name</b>';
$formerror = "<b>Please correct the errors on the form before proceeding</b>";
}

if (!empty($_POST['SURNAME'])) {
$surname = $_POST['SURNAME'];
}else{
$surname = NULL;
$surnamerror = '<b>** Please enter a Surname</b>';
$formerror = "<b>Please correct the errors on the form before proceeding</b>";
}

if (!empty($_POST['EMAIL'])) {
$email = $_POST['EMAIL'];
}else{
$email = NULL;
$emailerror = '<b>** Please enter an Email address</b>';
$formerror = "<b>Please correct the errors on the form before proceeding</b>";
}
// If everything is filled out send the e-mail and re-direct.
if($firstname && $surname && $email) {
{ 

// Configuration Settings
$SendFrom =    "New Group Booking <bookings@blairdrummond.com>";
$SendTo =      "bookings@blairdrummond.com";
//$SendTo =      "canary2k@hotmail.com";
$SubjectLine = "You have received a new group booking, details enclosed";
$ThanksURL =   "group_thanks.php";  //confirmation page
$Divider =     "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";

// Build Message Body from Web Form Input
$MsgBody = @gethostbyaddr($_SERVER["REMOTE_ADDR"]) . "\n$Divider\n";
foreach ($_POST as $Field=>$Value)
if(is_array($Value)) {
	$MsgBody .= "$Field: {$Value[0]}/{$Value[1]}/{$Value[2]}\n";
} else {
   $MsgBody .= "$Field: $Value\n";
   
}
$MsgBody .= "$Divider\n" . $_SERVER["HTTP_USER_AGENT"] . "\n";
$MsgBody = htmlspecialchars($MsgBody);  //make content safe

// Send E-Mail and Direct Browser to Confirmation Page
ini_set("sendmail_from",$SendFrom);
if(mail($SendTo, $SubjectLine, $MsgBody, "From: $SendFrom"))
header("Location: $ThanksURL");
else
die("a fatal error occured: line 46");

}
}
}

?>

 

But it still doesnt go to that $ThanksURL  ??? ???

Link to comment
Share on other sites

Well, apart from anything else, $ThanksURL is undefined.

 

You could also try telling us what it does do rather than leaving us to guess.

 

Is it !?

 

$ThanksURL =  "group_thanks.php";  //confirmation page

 

I'll tell you what the form needs to do, Maybe in further detail than my original post...

 

The form is in a sense an E-Mail contact form which calculates how many people are to go on a coach journey

 

I've done the validation for the form as it had none and was sending blank values to the end E-Mail address

 

The Validation works, Its just now an IF statement (I think) is required to check and confirm that all the entries are filled in and then proceed to the E-Mail bit which is located just below the if ($firstname && $surname ) etc

Link to comment
Share on other sites

At present, The script runs as in : FirstName is left blank the Validation kicks in and that Error Message goes through, So the validation works and the script in that sense works

 

When the fields are filled in and the script is run and the Submit button is pressed, All that happens is the page reloads and nothing else, Obviously what I'd like it do is send the E-Mail and also re-direct to that $ThanksURL

 

Just spotted your edit..... I dont get any error messages at all, Or none that I can see, It just reloads the form

 

Been at this all day  >:(

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.