Enter Image Text
<input name="captcha" type="text">
<img src="captcha.php" /><br>
into your form. The img tag calls captcha.php which outputs the image that contains the captcha. I donât see how youâll validate it though, unless itâs storing the value somewhere that you then look up in your form processing.
So, is it possible to integrate it into the contact_form.php? Hereâs that code:
<?php
$data = json_decode(file_get_contents("php://input"));
$name = trim($data->name);
$name = str_replace(array("\r", "\n"), array(" ", " "), $name);
$email = filter_var(trim($data->email), FILTER_SANITIZE_EMAIL);
$message = trim($data->message);
// Check that data was sent.
if (empty($name) || empty($message) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "One or more invalid entries. Please try again.";
exit;
}
$to = "support@...com";
$from = "From: contact-form@...com". "\r\n";
$body = "A message has been sent via the website contact form.\n\n";
$body .= "Name: $name\n";
$body .= "Email: $email\n\n";
$body .= "Message:\n$message\n";
if (mail($to, 'Customer Inquiry', $body)){
echo "Thank You. Your Message Has Been Sent.";
} else {
echo "An error has occurred and your message could not be sent.";
}
?>
Ah, I see how itâs doing it - when the captcha.php script generates the image, it sets a session variable called $_SESSION['code'] containing the value it needs to be. So you need to merge the extra bits into your contact_form.php
Add the session_start() to the start of your code (just before the second line).
Add the check for the value from validate to your own validation - you could do this separately by copying the exact code from the sample before or after your own check, or you could just merge it in and give the same error message whatever they do wrongly.
That should do it, come back if you have problems with it.
Yes, I could use a little more help.
I merged the code together, as suggested, but after I tested/completed the Form and entered the correct Captcha code, the Form info sends successfully, but the message shows:
âThank You. Your Message Has Been Sent.Wrong Code Enteredâ
Any additional help wil be appreciated. Hereâs the merged code:
<?php
session_start();
$data = json_decode(file_get_contents("php://input"));
$name = trim($data->name);
$name = str_replace(array("\r", "\n"), array(" ", " "), $name);
$email = filter_var(trim($data->email), FILTER_SANITIZE_EMAIL);
$message = trim($data->message);
// Check that data was sent.
if (empty($name) || empty($message) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "One or more invalid entries. Please try again.";
exit;
}
$to = "support@...com";
$from = "From: contact-form@...com". "\r\n";
$body = "A message has been sent via the website contact form.\n\n";
$body .= "Name: $name\n";
$body .= "Email: $email\n\n";
$body .= "Message:\n$message\n";
if (mail($to, 'Customer Inquiry', $body)){
echo "Thank You. Your Message Has Been Sent.";
} else {
echo "An error has occurred and your message could not be sent.";
}
if(isset($_POST["captcha"])&&$_POST["captcha"]!=""&&$_SESSION["code"]==$_POST["captcha"])
{
echo "Correct Code Entered";
//Do your stuff
}
else
{
die("Wrong Code Entered");
}
?>
Thanks for your reply.
Yes, it is âa bit jumbled upâ. Iâm hoping someone can help me un-jumble it.
I tried it this way(below), but, now I just get âWrong Code Enteredâ when I enter the correct code, and of course the Contact Form info does not send. Any additional guidance will be appreciated.
<?php
session_start();
$data = json_decode(file_get_contents("php://input"));
$name = trim($data->name);
$name = str_replace(array("\r", "\n"), array(" ", " "), $name);
$email = filter_var(trim($data->email), FILTER_SANITIZE_EMAIL);
$message = trim($data->message);
// Check that data was sent.
if (empty($name) || empty($message) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "One or more invalid entries. Please try again.";
exit;
}
if(isset($_POST["captcha"])&&$_POST["captcha"]!=""&&$_SESSION["code"]==$_POST["captcha"])
{
echo "Correct Code Entered";
//Do your stuff
}
else
{
die("Wrong Code Entered");
}
$to = "support@...com";
$from = "From: contact-form@...com". "\r\n";
$body = "A message has been sent via the website contact form.\n\n";
$body .= "Name: $name\n";
$body .= "Email: $email\n\n";
$body .= "Message:\n$message\n";
if (mail($to, 'Customer Inquiry', $body)){
echo "Thank You. Your Message Has Been Sent.";
} else {
echo "An error has occurred and your message could not be sent.";
}
?>
Not sure if this will make any difference. Try adding this bit of code just before the line that starts IF (isset($_POST['captcha']) ...
$capt = trim($data->captcha);
and change any reference to $_POST['captcha'] to use $capt instead. Iâve read a note that suggests that if you pass the form data through in JSON-encoded form, $_POST may not handle it correctly.
No, in this instance $capt is a simple variable, not an array, just like the other variables like $name that you extract in the same way at the top of the code. So we havenât replaced $_POST with $capt, weâve replaced $_POST['captcha'] with $capt. So lose the references to the array element of $capt in your if() check.
Your other note is a reasonable way to do it but the rest of your code already doesnât use the $_POST decoding method, so if you make that change youâll have to change the stuff at the top. Might make it easier to read.
I guess I donât understand from earlier âchange any reference to $_POST[âcaptchaâ] to use $capt insteadâ and âweâve replaced $_POST[âcaptchaâ] with $captâ, in regard to what exactly I need to change.
Exactly the same way that you display the contents of the $_SESSION array at the start of the code - using var_dump(). Incidentally now itâs clear that the session variables contain what you expect, you can remove that line to stop it interfering with the display.
If thereâs nothing in $capt - can you show the code that assembles the form data and sends it via ajax to your contact_form.php file? I am assuming thatâs how the form is submitted as the form tag has no âactionâ tag, and I do wonder whether the javascript code has to be modified to make it gather and submit the extra field youâve added.
Regarding âI do wonder whether the javascript code has to be modified to make it gather and submit the extra field youâve addedâ, yes, Iâve gotten some off-forum help with the js and it works successfully now.
Thanks again for your replies.