...

View Full Version : php email form checks required fields but still emails if they are empty



ramboangel11
06-01-2011, 06:09 AM
I have searched all over for the solution to my problem!


<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);


if(isset($_POST['Submit'])){

$myemail = "myemail@email.com";
$subject = "Update Resident Information";


$name = check_input($_POST['name'], "Enter your name");
$address = check_input($_POST['address'], "Enter your address");
$city = check_input($_POST['city'], "Enter your city");
$state = check_input($_POST['state'], "Enter your state");
$zip = check_input($_POST['zip'], "Enter your zip");
$home = check_input($_POST['home'], "Enter your home phone number");
$work = check_input($_POST['work'], "Enter your work phone number");
$mobile = check_input($_POST['mobile'], "Enter your mobile phone number");
$email = check_input($_POST['email'], "Enter your email address");

$emergname = check_input($_POST['emergname']);
$emergaddress = check_input($_POST['emergaddress']);
$emergcity = check_input($_POST['emergcity']);
$emergstate = check_input($_POST['emergstate']);
$emergzip = check_input($_POST['emergzip']);
$emerghome = check_input($_POST['emerghome']);
$emergwork = check_input($_POST['emergwork']);
$emergmobile = check_input($_POST['emergmobile']);
$emergemail = check_input($_POST['emergemail']);


/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}



$message = "Hello!

An update to resident information has been received from:

Your Information
Name: $name
Address: $address
City: $city
State: $state
Zip: $zip
Home Phone Number: $home
Work Phone Number: $work
Mobile Phone Number: $mobile
Email Address: $email

Emergency Contact Information
Name: $emergname
Address: $emergaddress
City: $emergcity
State: $emergstate
Zip: $emergzip
Home Phone Number: $emerghome
Work Phone Number: $emergwork
Mobile Phone Number: $emergmobile
Email Address: $emergemail
";

mail($myemail, $subject, $message);
}


function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}

function show_error($myError)
{
?>

Then I echo $myError in my content. It returns the error that is listed, ie Enter your name, yet it still sends an email with the required fields as blank. How can I make this not send the email until all required fields are filled out?

bullant
06-01-2011, 06:16 AM
One option:

1) set a boolean that denotes whether any of the user inputs is invalid or not.


$isDataValid = true;

2) then as you validate each input, if any input is invalid set $isDataValid = false;

3) then when you want to send the email


if($isDataValid) {
mail(.............);
}

ramboangel11
06-01-2011, 06:47 AM
like this? i thought that's what the check_input is for. i'm confused....



if ($name == TRUE)
{ echo "thank you";}
else
{echo "complete the field";}

bullant
06-01-2011, 06:56 AM
I was just describing the general concept based on what I normally do.

Also, it looks like you are just checking if the input has zero length or not which is not really sufficient. For example, if I entered qwerty as a phone number your check_input() would accept it as valid. You really need to check if each input has any invalid characters in it. But that is beside the point.

What you could do, is set $isDataValid = true; before any validation.

Then if your check_input() detects any invalid input data, the function then sets $isDataValid = false; and display any error message. ( Normally I store error messages in an array and then display them at the end of the validation.)

Then when you want to send the email, you first check if $isDataValid is true or false.

bullant
06-01-2011, 07:10 AM
Or something like this pseudo code maybe? Normally I have a php class to do the validation, but using procedural coding for simplicity -


<?php
$name = trim($_POST['txtName']);
$phone = trim($_POST['txtPhone']);

$errors = array();

if(!chkName($name)) {
$errors[] = 'Invalid name';
}

if(!chkPhone($phone)) {
$errors[] = 'Invalid phone no.';
}

if(count($errors) > 0){ //errors occurred
foreach($errors as $str) {
echo $str.'<br />';
}
} else { //all input data is valid
mail(....); //send email
}

function chkName($name){
//returns tru or false
}
function chkPhone($phone){
//returns tru or false
}
?>

ramboangel11
06-01-2011, 03:23 PM
This method is instead of check_input? it does make more sense...


Or something like this pseudo code maybe? Normally I have a php class to do the validation, but using procedural coding for simplicity -


<?php
$name = trim($_POST['txtName']);
$phone = trim($_POST['txtPhone']);

$errors = array();

if(!chkName($name)) {
$errors[] = 'Invalid name';
}

if(!chkPhone($phone)) {
$errors[] = 'Invalid phone no.';
}

if(count($errors) > 0){ //errors occurred
foreach($errors as $str) {
echo $str.'<br />';
}
} else { //all input data is valid
mail(....); //send email
}

function chkName($name){
//returns tru or false
}
function chkPhone($phone){
//returns tru or false
}
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum