I'm sure someone will be able to spot the problem with this. The captcha works fine, the email sends out if all is good (I still need to add field validation) but for some reason, the input fields aren't getting sent.
Can someone take a peek and see where I'm going wrong? Also, any suggestions for improvements to the code would be completely welcome ๐ I'm much more of a front end developer than a programmer.
Thanks!
gen
<?php
function createForm(){
/*grab variables from posted form, if any exist*/
$name = $_POST['name'];
$email = $_POST['email'];
$comments = $_POST['comments'];
/*build the form, populate the fields if needed*/
echo "<form action=\"" . $_SERVER['PHP_SELF'] . "\" method=\"POST\">
<p>Name:<br />
<input name=\"name\" type=\"text\" value=\"$name\" /></p>
<p>Email:<br />
<input name=\"email\" type=\"text\" value=\"$email\" /></p>
<p>Message:<br />
<textarea name=\"comments\" rows=\"4\">$comments</textarea></p>
<p class=\"smaller\">Spam Protection: enter the sum of the math question below<br />
<p><img src=\"inc/captcha.php\" alt=\"CAPTCHA image\" /> = <input type=\"text\" name=\"secure\"></p>
<p>
<label>
<input type=\"submit\" name=\"submit\" id=\"submit\" value=\"Submit\" />
</label>
<input type=\"reset\" name=\"reset\" id=\"reset\" value=\"Clear\" />
</p>
</form>";
}
if(isset($_POST['secure'])) {
if($_POST['secure'] != $_SESSION['security_number']) {
$error = "Failed";
echo "<p class=\"strong bigger red\">Woops! Wrong math answer, please try again.</p>";
createForm();
} else {
$error = "Passed";
/* recipients email address */
$recipient = 'my@email.com';
/* All form fields are automatically passed to the PHP script through the array $HTTP_POST_VARS. */
$name = $HTTP_POST_VARS['name'];
$email = $HTTP_POST_VARS['email'];
$comments = $HTTP_POST_VARS['comments'];
/*format the message for mail*/
$comments = stripslashes($comments);
$now = date('l F dS, Y @ g:i:s a');
$message = "The following is a message from website.com sent on $now.\n\nFrom: $name\nEmail: $email\n\nComments: \n$comments";
/*add email subject and headers*/
$subject = "website email";
$headers = "From: $name <$email>\n";
$headers .= "Reply-To: $email\r\n";
/* Sends the mail and outputs the "Thank you" string if the mail is successfully sent, or the error string otherwise. */
mail($recipient,$subject,$message,$headers);
echo "<p class=\"strong red\">Your email has been sent!</p>
<p>I'll do my best to get back to you in the next 24 hours.</p>";
/*for troubleshooting - output email message body after POST*/
//echo $message;
echo "<p><a href=\"website.com">Send another message ยป</a></p>";
}
} else { /*boot back to beginning*/
echo "<p>Send me a message using the below form. All fields are required.</p>";
createForm();
}
?>