add captcha to external form
Permalink
Hi there
After testing around for a while, I reached the point to ask you guys.
How the hack can I add a captcha validation to an external form?
myForm.php
This adds the Captcha. Great. But how to validate it?
In the Controller
This returns nothing. The Captcha is never validated...
Thanks a lot.
After testing around for a while, I reached the point to ask you guys.
How the hack can I add a captcha validation to an external form?
myForm.php
$captcha = Loader::helper('validation/captcha'); <?php $captcha->display(); ?> <?php $captcha->showInput(); ?>
This adds the Captcha. Great. But how to validate it?
In the Controller
$captcha = Loader::helper('validation/captcha'); echo "captcha: " . $captcha->check($_REQUEST['ccmCaptchaCode']);
This returns nothing. The Captcha is never validated...
Thanks a lot.
There must have been a typing error or somethink like this.
Thanks huntman. It works like a charm.
Thanks huntman. It works like a charm.
Can u post what you have in your controller? I am unable to get this to validate the captcha
What version of C5 are you using?
5.7
I have this in the controller
and in the view I have
$captcha = Core::make("captcha"); if (!$captcha->check()) { $this->error->add('Incorrect Captcha'); }
and in the view I have
<div class="form-group"> <label class="control-label"><?php echo $captcha->label(); ?></label> <div><?php $captcha->display(); ?></div> <div><?php $captcha->showInput(); ?></div> </div>
Hmm, I am getting a C5 error. Here's my action in the controller....
public function action_contact_us($bID = false) { $captcha = Core::make("captcha"); if (!$captcha->check()) { $this->error->add('Incorrect Captcha'); } if ($this->bID == $bID) { foreach($_POST as $key=>$value) { if($key!=="send") { $kkey = str_replace('_', ' ', $key); if(is_array($value)){ $value = implode(', ', $value); }
Viewing 15 lines of 38 lines. View entire code block.
What is the error you are getting?
I attached a screenshot. It seems that it's not even seeing the Captcha. If I take out the captcha, the form processes fine.
2 things:
1) Do you have use Core; at the top of the controller?
2) Can you change your debug settings to see the actual error?
1) Do you have use Core; at the top of the controller?
2) Can you change your debug settings to see the actual error?
I assume it doesn't matter the captcha? It's the "I'm not a robot" one. Also, I have $captcha = Loader::helper('validation/captcha'); in the top of the view.
This is what's above my action in the top of the controller....
<?php namespace Application\Block\ExternalForm\Form\Controller; use Concrete\Core\Controller\AbstractController; use Loader; class ContactFormPopup extends AbstractController {
Remove the use Loader; and put in use Core; loader is deprecated and shouldn't be used anymore.
Changed that and still got the error. Here's the debug....
Call to a member function add() on a non-object /home/bankofel/public_html/application/blocks/external_form/form/controller/contact_form_popup.php { public function action_contact_us($bID = false) { $captcha = Core::make("captcha"); if (!$captcha->check()) { $this->error->add('Incorrect Captcha'); }
Ok, so the issue is that you don't have an error object to add an error to, the captcha is incorrect. This code should really be inside of your if, not before it, otherwise it's probably getting called on the second form submit.
I don't get it, still not working :(
<?php namespace Application\Block\ExternalForm\Form\Controller; use Concrete\Core\Controller\AbstractController; use Core; class ContactFormPopup extends AbstractController { public function action_contact_us($bID = false) { if ($this->bID == $bID) { $captcha = Core::make("captcha"); if (!$captcha->check()) { $this->error->add('Incorrect Captcha'); } foreach($_POST as $key=>$value) {
Viewing 15 lines of 49 lines. View entire code block.
<?php namespace Application\Block\ExternalForm\Form\Controller; use Concrete\Core\Controller\AbstractController; use Core; class ContactFormPopup extends AbstractController { public function action_contact_us($bID = false) { if ($this->bID == $bID) { $captcha = Core::make("captcha"); if ($captcha->check()) { foreach($_POST as $key=>$value) {
Viewing 15 lines of 54 lines. View entire code block.
Dang, was hopeful with that.
Whoops \ Exception \ ErrorException (E_RECOVERABLE_ERROR) Object of class Concrete\Core\Error\Error could not be converted to string /home/bankofel/public_html/application/blocks/external_form/form/contact_form_popup.php <div class="head_contact_form"> <div class="container"> <div class="row"> <div class="col-md-6 col-sm-6 col-xs-12 col-md-offste-3 col-sm-offset-3"> <div class="form_out"> <div class="alert alert-danger" style="display:none;"><?php echo $error?></div> <?php if (isset($response)) { ?> <div class="alert alert-info"><?php echo $response?></div>
Change this line
to
<div class="alert alert-danger" style="display:none;"><?php echo $error?></div>
to
<div class="alert alert-danger" style="display:none;"><?php $error->output(); ?></div>
Man, didn't think this would be so darn difficult!
<?php $error->output(); ?> completely blows up my site :(
<?php $error->output(); ?> completely blows up my site :(
What does "completely blows up my site" mean? Gives you an error? Gives you a white screen?
It looks similar to the other error
Weird that I can't load any page on the site either.
Exception Occurred: /home/bankofel/public_html/application/blocks/external_form/form/contact_form_popup.php:13 Call to a member function output() on a non-object (1)
Weird that I can't load any page on the site either.
I'm guessing you get this when there are no errors, try this
<?php if($error){ ?> <div class="alert alert-danger" style="display:none;"><?php $error->output(); ?></div> <?php } ?>
Ok, well that got rid of that. However, I can still submit the form without checking the "I am not a robot" checkbox. That doesn't seem right...
Can you just post ALL of your code so we can see what we're dealing with.
Sure, here's the view code
And here's the controller
<div class="head_contact_div" style="display:none;"><?php $form = Loader::helper('form'); $captcha = Loader::helper('validation/captcha'); defined('C5_EXECUTE') or die("Access Denied."); ?> <div class="head_contact_form"> <div class="container"> <div class="row"> <div class="col-md-6 col-sm-6 col-xs-12 col-md-offste-3 col-sm-offset-3"> <div class="form_out"> <!--<div class="alert alert-danger" style="display:none;"><?php //echo $error?></div>--> <?php if($error){ ?> <div class="alert alert-danger" style="display:none;"><?php $error->output(); ?></div> <?php } ?> <?php if (isset($response)) { ?>
Viewing 15 lines of 147 lines. View entire code block.
And here's the controller
<?php namespace Application\Block\ExternalForm\Form\Controller; use Concrete\Core\Controller\AbstractController; use Core; class ContactFormPopup extends AbstractController { public function action_contact_us($bID = false) { if ($this->bID == $bID) { $captcha = Core::make("captcha"); if ($captcha->check()) { foreach($_POST as $key=>$value) {
Viewing 15 lines of 54 lines. View entire code block.
This code works fine for me if I remove the display: none; from the error->output div and the head_contact_div
Thanks for all your help but it just isn't working. I have to have those hidden. The form is in a modal. I don't understand why it still allows the form to submit if the captcha fails.
Me too.
I am wondering if maybe an AJAX submission would be better since this is in a modal? Problem is that on submit, the modal is closing before the captcha has a chance to validate. Any grand ideas about this?
I am wondering if maybe an AJAX submission would be better since this is in a modal? Problem is that on submit, the modal is closing before the captcha has a chance to validate. Any grand ideas about this?
When the form gets posted the page reloads, meaning your modal is gone? If so then perhaps AJAX is the way to go.
Thanks for sharing. This really helped.
It will find the correct captcha stuff in the post information and validate it.