Forum Moderators: coopster
However, if I enter a phone number and leave the email field blank I get the message: "You have entered an incorrect email address."
If I enter a phone number and an email I get this message: "Please provide an email address or a phone number so that we can contact you."
Also, if an email address is entered I get a message: "You have entered an incorrect email address."
Can someone tell me what I'm doing wrong. I have spent several days changing stuff around, googling to find an answer. but now I'm stumped.
Also, if there is anything that seems out of place, please let me know.
Thanks in advance.
if(!empty($_POST['Email']) && !empty($_POST['Phone'])) {
$status .= "Please provide an email address or a phone number so that we can contact you.<br />";
}
elseif ($email = eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z] {2,3})$", $_POST['Email'])) {
$status .= "You have entered an incorrect email address.<br />";
}
else {
$status .= "You have entered an incorrect email address.<br />";
}
if (empty($_POST['Name'])) {
$status .= "Please enter your name.<br />";
} else {
$name = stripslashes($_POST['Name']);
}
if (empty($message)) {
$status .= "Please enter a message.<br />";
} else {
$message = nl2br(stripslashes($_POST['Message']));
}
if (empty($status)) {
$to = 'myemail@mywebsite.com';
$subject = 'the subject';
$header = 'From: webmaster@example.com' . "\r\n" . 'Reply-To: webmaster@example.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion();
$message = "
Enquiring About: $department<br />
Name: $name<br />
Email: $email<br />
Phone: $phone<br />
Contact Preference: $contactpreference<br />
Message: $message";
$header = "From: $email\r\n";
$header .= "Reply-To: $emailrn";
$header .= 'X-Mailer: PHP/' . phpversion();
if(mail($to, $subject, $message, $header)) {
$status = "Thank you for your Feedback!<br><br>";
} else {
$status = "There was a problem sending your feedback, please contact the administrator.<br><br>";
}
}
}
?>
You need to run the individual checks for valid email and for valid telephone number only if they each are non-blank.
You also need to return an error message if both are blank.
However, first things first. You really need to have a good feel for logic in Control Structures [php.net] as that is fundamental to any type of programming, PHP or otherwise.
if (this is true) {
// do these statements
} else {
// it returned false, do these statements
} The logic in my head is:
If email OR phone is not entered show this message:
"Please provide an email address or a phone number so that we can contact you."
If email is entered check if valid, but then phone is not required.
If phone is entered, email is not required so no need to check if valid.
That's my logic but getting it in code is a struggle. Any help will be appreciated.
----------------------------------------------
if(!empty($_POST['Email']) && !empty($_POST['Phone'])) {
$status .= "Please provide an email address or a phone number so that we can contact you.<br />";
} else {
$email = eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z] {2,3})$", $_POST['Email']);
}
// Initialize some fields:
$error = array();
// Check if POST fields are set. If so, trim, otherwise initialize
$email = isset($_POST['Email']) ? trim($_POST['Email']) : '';
$phone = isset($_POST['Phone']) ? trim($_POST['Phone']) : '';
// Do some validation:
if (!$email ¦¦ !isValidInetAddress [webmasterworld.com]($email) {
$error[] = 'You have entered an incorrect email address.<br />';
}
if (!$phone) {
$error[] = 'You have entered an incorrect phone number.<br />';
}
// No errors?
if (!$error) {
// process the successful form submission
}
// Display errors:
$errors = implode('</li><li>', $error);
print "You have errors: <ul><li>$errors</li></ul>";
Immediately before the email-only format check I would have added a line 'IF email is non-blank do this check".
Immediately before the phone-only format check I would have added a line 'IF phone is non-blank do this check".
And now that I look a bit closer, I also realize you are indeed using an indicator of sorts, the $status variable. Should have grabbed some morning coffee first I guess :P
// Do some validation:
if (!$email && !$phone) {
$error[] = "Please provide an email address or a phone number so that we can contact you.<br />";
} else {
if ($email && !isValidInetAddress [webmasterworld.com]($email) {
$error[] = 'You have entered an incorrect email address.<br />';
}
if (!$email && !isInvalid($phone)) {
$error[] = 'You have entered an incorrect phone number.<br />';
}
}
Although I removed the "empty" part of the code, which I assume is what is checking if the field is empty or not, it's still prompting me to enter an email. I'm not sure why that is happening?
if(!ereg("^([A-Za-z_\.]*)@([A-Za-z_]*)\.([A-Za-z_\.]*)$", $email)) {
$status .= "You have entered an incorrect email address.<br />";
} else {
$name = stripslashes($_POST['Email']);
}
Thanks in advance!
read about the Ternary operator [php.net]
I'm not giving up on my original code which is to check if an email OR phone number is entered
The user has the option to enter either an email address or a phone number, if ONE of those are entered then the form should be sent.
Please go back and review the code samples offered. You don't have to use the exact same code, but the logic is there to get you started. Replace the second "// Do some validation:" section in the first snippet with the updated snippet after corrections were made that were mentioned/caught by g1smd.