I'm sure this has been asked if it has please point me in the right direction as I can't seem to find it thanks.
Pretty much as the title says how many else if statements are you allowed? I have this contact form that spits out an error if someone using anything other than letters in the name and subject field.
When I remove the preg_match for my subject field so they can put anything they want it works fine but when I add it back in even when they use only letters it still says 'Subject field must only contain letters', I have a feeling it is because you're only allowed one else if statement. If this is the case how would I go about showing the error_subject message for the subject field and error_name for the name field?
I appreciate any bit of help :) Thanks
PHP Contact Form Code:
<?php
//Response Generation Function
$response = "";
//Function To Generate Response
function my_contact_form_generate_response($type, $message){
global $response;
if($type == "success") $response = "
<div class='success-message'>
<div class='success'>{$message}</div>
</div>";
else $response = "
<div class='error-message'>
<div class='error'>{$message}</div>
</div>";
}
//Response Message
$not_human = "Human verification incorrect.";
$missing_content = "Please fill in all required fields.";
$error_name = "Full Name field must only contain letters";
$error_subject = "Subject field must only contain letters";
$email_invalid = "E-Mail Address Invalid.";
$message_unsent = "Message was not sent. Try Again.";
$message_sent = "Thanks! Your message has been sent.";
//User Posted Variable
$name = $_POST['message_name'];
$email = $_POST['message_email'];
$tele = $_POST['message_tele'];
$subject = $_POST['message_subject'];
$message = $_POST['message_text'];
$human = $_POST['message_human'];
//PHP Mailer Variables
$to = 'stephen@e-foreknowledge.co.uk';
$subject = "New Message - ".get_bloginfo('name');
$headers = "From: $email
";
$body =
"Name: $name
Subject: $subject
Telephone: $tele
Message: $message
";
if(!$human == 0){
if($human != 2) my_contact_form_generate_response("error", $not_human); //not human!
else {
//Validate Email
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
my_contact_form_generate_response("error", $email_invalid);
else //Email is Valid
{
//Validate Presence of Name and Message
if(empty($name) || empty($tele) || empty($subject) || empty($message)){
my_contact_form_generate_response("error", $missing_content);
}
else if(!preg_match("/^[a-zA-Z ]*$/", $name)) {
my_contact_form_generate_response("error", $error_name);
}
else if(!preg_match("/^[a-zA-Z ]*$/", $subject)) {
my_contact_form_generate_response("error", $error_subject);
}
else {
$sent = wp_mail($to, $subject, $headers, $body);
if($sent) my_contact_form_generate_response("success", $message_sent); //Message Sent!
else my_contact_form_generate_response("error", $message_unsent); //Mssage Wasn't Sent
}
}
}
}
else if ($_POST['submitted']) my_contact_form_generate_response("error", $missing_content);
?>
This is for my WordPress site, I'm making a custom contact page with a contact form inside. The answer is below if you have the same problem I was having :)
So the problem is I was calling the $subject in two different place that meant two different things and they clashed. I was using one for the subject that is shown within an E-Mail and the other was for the subject field on the contact page. The fixed code is below :)
<?php
//Response Generation Function
$response = "";
//Function To Generate Response
function my_contact_form_generate_response($type, $message){
global $response;
if($type == "success") $response = "
<div class='success-message'>
<div class='success'>{$message}</div>
</div>";
else $response = "
<div class='error-message'>
<div class='error'>{$message}</div>
</div>";
}
//Response Message
$not_human = "Human verification incorrect.";
$missing_content = "Please fill in all required fields.";
$error_name = "Full Name field must only contain letters";
$error_subject = "Subject field must only contain letters";
$email_invalid = "E-Mail Address Invalid.";
$message_unsent = "Message was not sent. Try Again.";
$message_sent = "Thanks! Your message has been sent.";
//User Posted Variable
$name = $_POST['message_name'];
$email = $_POST['message_email'];
$tele = $_POST['message_tele'];
$formsubj = $_POST['message_subject'];
$message = $_POST['message_text'];
$human = $_POST['message_human'];
//PHP Mailer Variables
$to = 'stephen@e-foreknowledge.co.uk';
$subject = "New Message - ".get_bloginfo('name');
$headers = "From: $email
";
$body =
"Name: $name
Subject: $formsubj
Telephone: $tele
Message: $message
";
if(!$human == 0){
if($human != 2) my_contact_form_generate_response("error", $not_human); //not human!
else {
//Validate Email
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
my_contact_form_generate_response("error", $email_invalid);
else //Email is Valid
{
//Validate Presence of Name and Message
if(empty($name) || empty($tele) || empty($formsubj) || empty($message)){
my_contact_form_generate_response("error", $missing_content);
}
else if(!preg_match("/^[a-zA-Z ]*$/", $name)) {
my_contact_form_generate_response("error", $error_name);
}
else if(!preg_match("/^[a-zA-Z ]*$/", $formsubj)) {
my_contact_form_generate_response("error", $error_subject);
}
else {
$sent = wp_mail($to, $subject, $headers, $body);
if($sent) my_contact_form_generate_response("success", $message_sent); //Message Sent!
else my_contact_form_generate_response("error", $message_unsent); //Mssage Wasn't Sent
}
}
}
}
else if ($_POST['submitted']) my_contact_form_generate_response("error", $missing_content);
?>
As many as you want. There is no limit on any programming construct in any language. The limit is disk space and memory usage.
Your problem is here:
$subject = "New Message - ".get_bloginfo('name');
you actually insert a hyphen (-
) in the subject yourself.