What do you mean by “unable”, @Atik1? Do you mean you don’t know how to do it, or do you mean you’ve tried to do it but it is not working as expected?
You should know by now that you need to give full details of your issue in order to receive any useful replies.
You’re the one looking for assistance, so the onus is upon you to explain clearly what it is that you are asking, and what help you want.
I’m sure I’m not the only member getting very tired of trying to guess what you’re asking, only to have suggestions dismissed because you have requirements you have failed to mention.
This is the processing part of your form with some changes.
<?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>";
else $response = "<div class='error'>{$message}</div>";
return $response ; // add return, the function must return something
}
//response messages
$not_human = "Human verification incorrect.";
$missing_content = "Please supply all information.";
$email_invalid = "Email Address Invalid.";
$message_unsent = "Message was not sent. Try Again.";
$message_sent = "Thanks! Your message has been sent.";
if($_SERVER['REQUEST_METHOD'] == 'POST'){ // add this important test
//user posted variables
$name = $_POST['message_name'];
$email = $_POST['message_email'];
$message = $_POST['message_text'];
$human = $_POST['message_human'];
//php mailer variables
$to = get_option('admin_email');
$subject = "Someone sent a message from Outer Space";
$headers = 'From: '. $email . "\r\n" .
'Reply-To: ' . $email . "\r\n";
if($human != ''){ // condition changed to check empty string or not
if($human != 2) { $response = my_contact_form_generate_response("error", $not_human); } // add brackets //not human! // $response =
else {
//validate email
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$response = my_contact_form_generate_response("error", $email_invalid); // add brackets // $response =
}
else //email is valid
{
//validate presence of name and message
if(empty($name) || empty($message)){
$response = my_contact_form_generate_response("error", $missing_content); // $resp =
}
else //ready to go!
{
$sent = mail($to, $subject, strip_tags($message), $headers);
if($sent) { $response = my_contact_form_generate_response("success", $message_sent); } //message sent! , add brackets // $response =
else { $response = my_contact_form_generate_response("error", $message_unsent); } //message wasn't sent, add brackets // $response =
}
}
}
}
elseif ($_POST['submitted']) {$response = my_contact_form_generate_response("error", $missing_content); } // elseif is one word, add brackets // $response =
} // end if POST
// move to top of page ^^
?>
The first thing to note is that this whole block of code should be moved to the beginning of the document, before any html.
There was a problem with your response function, it did not return anything.
Also when you called the function, you did not assign the result to the variable, though there was no result, being no return. In many cases you also had no brackets following the conditions before calling the function.
The form processor did not have any test to determine if any data had been posted. I added this condition for that:-
if($_SERVER['REQUEST_METHOD'] == 'POST'){...}
So the script only runs after a submission.
You were using this condition to check if the captcha had been filled in:-
if(!$human == 0){...}
I changed it for this to check for empty strings only:-
if($human != ''){ ...}
You may have used empty in the condition, but that would pick '0' as being empty.
It may not be the perfect form validation script, but at least it should be functional now.
Another method that you might want to also consider is the time taken to submit a form. When the user goes to the page with the form, get a timestamp and store it in the session data. Then when the form is submitted get the current timestamp and calculate the interval between the two timestamps. If the interval is less then a certain time, reject the submitted data and return a generic “Oops something went wrong” error message