invisible reCAPTCHA javascript - forms

I have the invisible reCAPTCHA set up, but it doesn't seem to want to call my callback function. My form looks like:
<form id='ContactAgentForm' name='ContactAgentForm' class='custom-form-widget-form standard_form' action='contact_agent' listing_id=1233445>
...
<div class='field captcha-field recaptcha_field' >
<div id='g-recaptcha-div' class="g-recaptcha" ></div>
</div>
...
<div class="field button-field">
<button class="button button-primary"><span>Send</span></button>
<a class="button button-cancel btn-close" href="#cancel"><span>Cancel</span></a>
</div>
</form>
In the javascript, I want to handle the fact that there might be multiple forms on the page, so I create a list of all the forms. For each form, I attach/render the reCAPTCHA logic, attaching my callback with the form passed as a parameter:
<script>
var $form_list = jQuery("form.custom-form-widget-form");
var onFormPageSubmit = function(token, $form ) {
console.log("Got here! ", token );
var field = $form.find('.g-recaptcha-response')[0];
field.value = token;
$form[0].submit();
};
var onloadCallback = function() {
$form_list.each( function() {
var $form = jQuery(this);
var $recaptcha = $form.find( ".g-recaptcha" );
if ( $recaptcha.length )
{
var recaptchaId = grecaptcha.render($recaptcha[0], {
'callback': function (token) { onFormPageSubmit(token, $form); },
'sitekey': "{$captcha_config.invisible_captcha_site_key}",
'size': 'invisible',
'badge': 'inline'
});
$form.data("recaptchaid", recaptchaId);
}
});
};
</script>
And just below that, I load the recaptcha/api.js file:
<script src="https://www.google.com/recaptcha/api.js?render=explicit&onload=onloadCallback"></script>
With some judicial 'console.log' statements, we get through all of the code EXCEPT for the callback (onFormPageSubmit). The "protected by reCAPTCHA" logo is there, but it seems that the form is just submitted, ignoring the reCAPTCHA call altogether.
All help appreciated.

Somewhere along the line, the validation function for the form was lost (it's in another file). The validation function was attached to the button, and it executed something like this:
$submit_button.on( 'click', function( event ) {
event.preventDefault();
// get the recaptchaid from form data
var $recaptcha_id = $form.data( "recaptchaid" );
if ( $recaptcha_id != undefined )
{
grecaptcha.execute($recaptcha_id);
}
} );
The "grecaptcha.execute" is the important thing - this is what triggers the actual reCAPTCHA call.

Related

ZF3 redirect()->toUrl() not redirecting

I'm having a weird issue with ZF3.
I have a vanilla form in the view and a jquery ajax to send it to the controller, something like this:
<form>some form</form>
<script>
$("#form").submit(function (e) {
e.preventDefault();
$.ajax({
method: "POST",
url: "stats",
data: {name: 'TEST'} // name selected in the form
});
});
</script>
The controller for action stats looks like this:
$stat = new Stat();
$route_name = $this->params()->fromRoute('name', 'none');
$post_name = $this->params()->fromPost('name', 'none');
if(!strcmp($route_name, 'none')) // if no redirection yet
{
if(!strcmp($post_name, 'none')) // if no form was sent
{
// display the form to choose the customer
return new ViewModel([
'customer_list' => $stat->get_customer_list(),
]);
}
else // if the form was sent, get name and direct to /stats/someName
{
return $this->redirect()->toRoute('stats', ['name' => 'someName']);
}
}
else // after redirection, get the name in the URL and show some data about this customer
{
return new ViewModel([
'avg_time' => $stat->get_avg_time(rawurldecode($route_name)),
]);
}
The problem is that the redirection does not occure on the screen but I still get the route parameter if I print $route_name after submitting the form.
Anyway, the goal is to have a form with a select to choose the customer name and load the customer data into /stats/[name]. Am I going in the wrong direction ? And is the redirection issue a bug or my code is wrong ?
So there I solved it thx to rkeet, this is the form & jquery:
<form id="customer_choice" method="POST" action=""> some form </form>
<script>
$("#customer_choice").submit(function () {
$("#customer_choice").attr('action', 'stats/' + $("#customer_select").val())
});
</script>
And this is the controller (hope no customer is named 'none'):
$stat = new Stat();
$name = $this->params()->fromRoute('name', 'none');
if(!strcmp($name, 'none'))
{
return new ViewModel([
'customer_list' => $stat->get_customer_list(),
]);
}
else
{
return new ViewModel([
'avg_time' => $stat->get_avg_time($name),
]);
}
The result is basepath/stats/[customer name] and changing the url manually works as well.
(if you don't want changing the url manually to change the result, use fromPost instead of fromRoute)

codeigniter form_validation with ajax

When i submit it with no data, it shows like this. but i want it to show on my modal, because i'm using the form_validation library.
enter image description here
This is my Controller:
$this->form_validation->set_rules('category_name','Category name','trim|required');
if($this->form_validation->run() == FALSE){
echo validation_errors();
} else{
$category = array(
"category_name" => $this->input->post('category_name')
);
$this->category_m->insert($category);
echo "Successfully Added.";
}
This is my ajax file:
$(document).on('submit', '#form' ,function(event){
event.preventDefault();
var form = $(this).serialize();
$.ajax({
url:"<?php echo base_url(); ?>category/operation",
type:"POST",
data:form,
success:function(data){
$('#form')[0].reset();
$('#mymodal').modal('hide');
$('#alert').fadeIn().html('<div class="alert alert-info">'+data+'</div>').fadeOut(5000);
table.ajax.reload();
}
})
});
This is not related to Codeigniter. It's JS/jQuery/HTML/...
Assuming that you have something like this in your HTML
<div id="mymodal">...</div>
Create another div#alert inside
<div id="alert">...</div>
And change the Javascript to something like
//You don't neet fadeIn/fadeOut
$('#alert').html('<div class="alert alert-info">'+data+'</div>');
$('#mymodal').modal('show');

Angular Form Submit

In my departments.component.html, I have this:
Focus on the #form and calling of addDepartment(form.formValues)
<app-dialog-form *ngIf="showDialogBox" [(showDialogBox)]="showDialogBox" #form [values]="department">
Register Department
<div class="action" action>
<button class="ui button basic" (click)="onToggleDialogBox()">Cancel</button>
<button class="ui button primary medium" (click)="addDepartment(form.formValues)">Save</button>
</div>
</app-dialog-form>
The getting of values in my formValues is this:
get formValues(): any {
let data;
data = this.dialogForm.value;
return data;
}
And here is my code for addDepartment()
addDepartment(department: Department) {
console.log(department.name);
if (typeof department === 'object') {
this.departmentService
.registerDepartment(department)
.subscribe(
res => { alert(res);
//this.goBackToDepartmentListPage();
},
error => alert(error)
)
}
}
But everytime, I submit the form values and try printing in the console the input, I get this error
Cannot read property 'value' of undefined at
DialogFormComponent.get [as formValues]

how to integrate the mailchimp with core php

I need to integrate the mail chimp api with core php I tried more type of codes but not working. Finally, I found the below code. I have created an api key with mailchimp, likewise campaigns, template and list in the mailchimp.
<script src="jquery.min.js" type="text/javascript"></script>
<script>
$(document).ready(function() {
$('#subscribe').submit(function() {
if (!valid_email_address($("#email").val()))
{
$(".message").html('The email address you entered was invalid. Please make sure you enter a valid email address to subscribe.');
}
else
{
$(".message").html("<span style='color:green;'>Adding your email address...</span>");
$.ajax({
url: 'subscribe.php',
data: $('#subscribe').serialize(),
type: 'POST',
success: function(msg) {
if(msg=="success")
{
$("#email").val("");
$(".message").html('<span style="color:green;">You have successfully subscribed to our mailing list.</span>');
}
else
{
$(".message").html('The email address you entered was invalid. Please make sure you enter a valid email address to subscribe.');
}
}
});
}
return false;
});
});
function valid_email_address(email)
{
var pattern = new RegExp(/^[+a-zA-Z0-9._-]+#[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/i);
return pattern.test(email);
}
</script>
<?php
$api_key = "key";
$list_id = "id";
require('inc/MAPI.class.php');
$Mailchimp = #new Mailchimp( $api_key );
echo bool($Mailchimp);
//$Mailchimp_Lists = #new Mailchimp_Lists( $Mailchimp );
//$subscriber = $Mailchimp_Lists->subscribe( $list_id, array( 'email' => htmlentities($_POST['email']) ) );
if ( ! empty( $subscriber['leid'] ) ) {
echo "success";
}
else
{
echo "fail";
}
?>
<div class="message"></div>
<form role="form" method="post" id="subscribe">
<input type="email" id="email" name="email" placeholder="you#yourself.com" value="">
<button type="submit">SUBSCRIBE</button>
</form>
This is my code, but it's not returning, errors only. Please help me.

Submitting the form after changing value in Angular Directive submits old value

http://jsfiddle.net/p8RNJ/3/
When I press "submit" button, fooController in controller is equal to 'A' so barController gets assigned value 'A', which is confirmed in UI:
bar in controller: A
After I press "set value" button, fooController is updated from directive through binding with value 'B', which is confirmed in UI:
foo in controller: B
However after I press "submit" button, fooController in controller still has old value of 'A' so $scope.barController once again gets assigned a value of 'A':
bar in controller: A
How can I ensure that on second submit fooController has correct value of 'B' and barController gets assigned a value of 'B' as well?
HTML:
<div ng-app="app" ng-controller="Controller">
<script type="text/ng-template" id="directive-template.html">
foo in directive: {{fooDirective}}<br/>
<button ng-click="setValue()">set value</button>
</script>
<form data-ng-submit="getModel()">
<div ng-controller="Controller">
<button type="submit">submit</button><br/>
foo in controller: {{fooController}}<br/>
bar in controller: {{barController}}<br/>
<my-customer model="fooController"></my-customer>
</div>
</form>
</div>
And the JS:
angular.module('app', [])
.controller('Controller', ['$scope', function($scope) {
$scope.fooController = 'A';
$scope.getModel = function() {
$scope.barController = $scope.fooController;
}
}])
.directive('myCustomer', function() {
return {
restrict: 'E',
scope: {
fooDirective: '=model'
},
controller: function ($scope) {
$scope.setValue = function() {
$scope.fooDirective = 'B';
};
},
templateUrl: 'directive-template.html'
};
});
Don't use primitive data types (number, strings, booleans) because the inheritance doesn't work both ways, it only works from parent $scope to chid $scope. If you want it to work both ways user objects or arrays (that's what the angular community actually recommends).
$scope.barController = "aaaa";
should be:
$scope.modelState.barController = "aaa";
This way if you change it from the child scope it will be updated in the parent one as well.
you need to wrap your directive change in $scope.apply
$scope.apply(function(){
$scope.setValue = function() {
$scope.fooDirective = 'B';
};
}