Recaptcha with CodeIgniter
I am trying to implement recaptcha my form in CodeIgniter (without using the recaptcha library). This works fine, however as in my form, I am displaying each fields errors individually, I want to display recaptcha error next to its place in the form, can someone help me how can i do it?
The code from my controller:
$this->form_validation->set_rules('name', 'Name', 'trim|required|min_length[3]|max_length[25]');
$this->form_validation->set_rules('email', 'Email address', 'trim|required|valid_email');
//With above set_rules i'm able to display each fields errors next to it,
//How can i display following recaptcha error next to it.
if (!$resp->is_valid)
{
//reCAPTCHA was entered incorrectly
die 开发者_JAVA技巧(
"The reCAPTCHA wasn entered incorrectly." .
"(reCAPTCHA said: " . $resp->error . ")
");
}
else
{
//Successful verification
die('Success!');
}
Thanks for any help.
If you aren't running the recaptcha field through the form validation library you won't be able to use the form_error()
function, but you can easily send the error to your view as a variable:
if ($this->form_validation->run())
{
if ( ! $resp->is_valid)
{
$data['recaptcha_error'] = "reCAPTCHA said: ".$resp->error;
}
else
{
// Process form
}
}
$this->load->view('my_view', $data);
Then in your view (wherever/however you want):
<?php if (isset($recaptcha_error)): ?>
<label class="error">
<?php echo $recaptcha_error; ?>
</label>
<?php endif; ?>
精彩评论