Jump to content

Need help with mail form


postonoh

Recommended Posts

I have this in a file called mailform.php. It check for valid email and phone number. If wrong it echo error but it still send the mail to me. I need for it if error not to send then redirect to same page with problems.

no error redirect to main page.

 

I can get to redirect  with no problem. I can't figure out how to make it not send if errors.

 

I hope I am explaining this right. 

 

 

$fname = $_POST['firstName'];
$lname = $_POST['lastName'];
$degree = $_POST['Degree_Certificate'];
$company = $_POST['companyName'];
$address = $_POST['street'];
$city = $_POST['city'];
$state = $_POST['stateProvReg'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$message = $_POST['description'];
$receive = "we receive your Affiliate information";
$formcontent=" Thank you $receive \n First Name: $fname \n Last Name: $lname \n Degree: $degree \n Company: $company \n Address: $address \n City: $city \n State: $state \n Phone: $phone \n Email: $email \n Description: $message";
$recipient = "webtechnician@brainmaster.com, $email";
$subject = "Affiliate List";
$mailheader = "From: $email \r\n";

if (validatePhone($phone))
{
echo $phone . ' is a valid phone number.';
} else {
echo $phone . ' is not valid phone number.';
}


if (check_email_address($email))
{
echo $email . ' is a valid email address.';
} else {
echo $email . ' is not a valid email address, please hit the back button.';
}


function check_email_address($email) {
    // First, we check that there's one @ symbol, and that the lengths are right
    if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
        // Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
        return false;
    }
    // Split it into sections to make life easier
    $email_array = explode("@", $email);
    $local_array = explode(".", $email_array[0]);
    for ($i = 0; $i < sizeof($local_array); $i++) {
         if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
            return false;

        }
    }    
    if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
        $domain_array = explode(".", $email_array[1]);
        if (sizeof($domain_array) < 2) {
                return false; // Not enough parts to domain
        }
        for ($i = 0; $i < sizeof($domain_array); $i++) {
            if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
                return false; 
		 die("hit the back button");
            }
        }
    }
    return true;
}

function validatePhone($phone) {
$numbersOnly = ereg_replace("[^0-9]", "", $string);
$numberOfDigits = strlen($numbersOnly);
if ($numberOfDigits == 7 or $numberOfDigits == 10) {
	echo $numbersOnly;
} else {
	echo 'Invalid Phone Number';
}
}

function valIP($string) {
if (preg_match(
'^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:[.](?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$^',
$string)) {
echo $string;
} else {
	echo 'Invalid IP Address';
}
}

mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
header('Location: http://#');

Link to comment
Share on other sites

What I am seeing is that you haven't set it so the email won't send if one or both of your requirements aren't met.

So you need it like

if (validatePhone($phone))
{

if (check_email_address($email))
{
send email
}

else
{
invalid email
}
}
else 
{
invalid phone number
}

So in that case the email will only send if both the phone and email are valid.

Link to comment
Share on other sites

In addition to howlin's example, you can also accomplish what you want with the following:

 

simply use exit to stop executing once you find an error. For example, your if statement for phone validation would become

if (validatePhone($phone))
{
echo $phone . ' is a valid phone number.';
} else {
echo $phone . ' is not valid phone number.';
        //not valid so exit
        exit("Email not sent");
}

 

you would obviously have to change your other validation if statements in a similar manner.

 

Some do not like this method, as if you exit execution at this point, other parts of the page will not load (including scripts you may need somewhere below this point.

 

So you can simply set a flag variable to true or false depending on the error. The change to your if statements would be similar.

 

$errors = false;//no errors. put this at top of page

...
..

if (validatePhone($phone))
{
echo $phone . ' is a valid phone number.';
} else {
echo $phone . ' is not valid phone number.';
        $errors = true;//there was an error. set the flag
}

... make same change for rest of validation functions

if (!$errors){//if theres no errors
mail (... your arguments here ...);
}

.. rest of script

 

Hope this helps

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.