- This topic is empty.
-
AuthorPosts
-
February 12, 2012 at 2:24 am #36617cybershotParticipant
I am having trouble with a bit of code. I can’t explain what is going on here. I am trying to modify some code to work on my site. I have the site looking correct but in order to do that, I had to remove a bit of code that prevented the site from sending an email when the forms fields are empty. Right now, it will send the form when they are empty. I have been trying to add some conditionals into the code to prevent it from sending but for some reason, it isn’t working and I can’t figure out why. Here are a few things I have tried.
this is my current attempt at setting an boolean variable. I have never really done this so it may be the wrong aproach.
$set = '0';
if(!isset($_POST) ||
!isset($_POST) ||
!isset($_POST)) {
$set = 0;
died('We are sorry, but there appears to be a problem with the form you submitted.');
} else {
$set = 1;
}
I have also tried
if(!isset($_POST) ||
!isset($_POST) ||
!isset($_POST)) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
die();
}
and this
if( $set == 1 ){
$headers = 'From: '.$email_from."rn".
'Reply-To: '.$email_from."rn" .
'X-Mailer: PHP/' . phpversion();
mail($email_to, $email_subject, $email_message, $headers);
}
no matter what I do, $set is always equal to 1. I can’t figure out why it won’t equal 0. The first conditional should make it zero but it doesn’t.
Here is the unedited code
if(isset($_POST)) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "[email protected]";
$email_subject = "Your email subject line";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.
";
echo $error."
";
echo "Please go back and fix these errors.
";
die();
}
// validation expected data exists
if(!isset($_POST) ||
!isset($_POST) ||
!isset($_POST) ||
!isset($_POST) ||
!isset($_POST)) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST; // required
$last_name = $_POST; // required
$email_from = $_POST; // required
$telephone = $_POST; // not required
$comments = $_POST; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.
';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid
';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid
';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid
';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.nn";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."n";
$email_message .= "Last Name: ".clean_string($last_name)."n";
$email_message .= "Email: ".clean_string($email_from)."n";
$email_message .= "Telephone: ".clean_string($telephone)."n";
$email_message .= "Comments: ".clean_string($comments)."n";
// create email headers
$headers = 'From: '.$email_from."rn".
'Reply-To: '.$email_from."rn" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
Thank you for contacting us. We will be in touch with you very soon.
}
?>
February 12, 2012 at 10:50 am #96578bungleMemberA post variable will be set even if it is empty, you need to test if they are empty instead. The trim() makes sure they haven’t just put blank space in there.
// validation expected data exists
if(trim($_POST)=="" ||
trim($_POST)=="" ||
trim($_POST)=="" ||
trim($_POST)=="" ||
trim($_POST)=="") {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}February 12, 2012 at 11:03 pm #96605bungleMemberI would alway try to eliminate the largest error first with a condition then narrow down and then do an else for the final action. That way the mail can only proceed if no errors have been flagged
Try this
if (isset($_POST) && trim($_POST)!=="") {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "[email protected]";
$email_subject = "Your email subject line";
function died($error) {
$diemessage="We are very sorry, but there were error(s) found with the form you submitted.
These errors appear below.
".$error."
Please go back and fix these errors.
";
die($diemessage);
}
$first_name = trim($_POST); // required
$last_name = trim($_POST); // required
$email_from = trim($_POST); // required
$telephone = trim($_POST); // not required
$comments = trim($_POST); // required
if($first_name=="" || $last_name=="" || $email_from=="" || $telephone=="" || $comments=="") {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.
';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid
';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid
';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid
';
}
if($error_message!=="") {
died($error_message);
}
else { //we didn't have an error to report so go on
$email_message = "Form details below.nn";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."n";
$email_message .= "Last Name: ".clean_string($last_name)."n";
$email_message .= "Email: ".clean_string($email_from)."n";
$email_message .= "Telephone: ".clean_string($telephone)."n";
$email_message .= "Comments: ".clean_string($comments)."n";
// create email headers
$headers = 'From: '.$email_from."rn".
'Reply-To: '.$email_from."rn" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
echo ('Thank you for contacting us. We will be in touch with you very soon.');
}
}
?>
February 12, 2012 at 11:10 pm #96606bungleMemberElse if you want to go the boolean route, encapsulate all the tests in a function and return true or false then you can call it easily to perform a total verification something structurally like
$error="";
function all_tests(){
if (insert test here for not valid email) {
$error="Email address incorrect.";
return false;
}
if (insert test here for not valid comment) {
$error="Invalid comment.";
return false;
}
// etc. etc. keep going with conditions.
return true;
}
if (all_tests()) {
send mail code;
}
else {
die('Sorry something went wrong as follows.
'.$error);
}
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.