mail form question

Links for php scripts

Moderators: macek, egami, gesf

New php-forum User
New php-forum User
Posts: 1
Joined: Thu Sep 29, 2011 7:53 am

mail form question

Postby postonoh » Thu Sep 29, 2011 7:55 am

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.

Code: Select all

$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 = ", $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(
   $string)) {
   echo $string;
   } else {
      echo 'Invalid IP Address';

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

Return to “PHP Scripts”

Who is online

Users browsing this forum: No registered users and 1 guest